'is there a way to have a doGet and doPost in the same servlet and get information from a form?

I am trying to get information from a html form and pass it to my doPost. inside my doPost it will be persisted into my postsql database. However, i seem to have problems sending the data from the forms, i am not sure how the doPost is getting the information or why it is not getting it.

This is my servlet with
public class WeddingServlet extends HttpServlet {

private final WeddingService weddingService;
private final ObjectMapper mapper;

public WeddingServlet(WeddingService weddingService, ObjectMapper mapper) {
    this.weddingService = weddingService;
    this.mapper = mapper;
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    resp.setContentType("application/json");
    try {
        Wedding newWedding = mapper.readValue(req.getInputStream(), Wedding.class);
        boolean wasRegistered = weddingService.addWedding(newWedding);
        if (wasRegistered) {
            resp.setStatus(200);
            resp.getWriter().write("Data Persisted");
        } else {
            resp.setStatus(500);
            resp.getWriter().write("data did not persist");
        }
    } catch (StreamReadException | DatabindException e) { // TODO: handle exception
        resp.setStatus(400);
        e.printStackTrace();
    } catch (Exception e) {
        resp.setStatus(500);
        e.printStackTrace();
    }

}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.getWriter()
            .write("<head><title>Save The Date !</title></head>"
                    + "<img src='./images/savethedate.png' alt='logos'/>" + "<body>" + "<h1> Home </h1>"
                    + "<div id='intro'> " + "<style type=text/css>" + "body {background-color: #79525C;}" + ""
                    + "</body>" + "</style>"
                            + ""
                            + "<form action=\"localhost:8080/wedding\" method=\"POST\">\r\n"
                            + "        Wedding date: <input type=\"text\" name=\"wedding_date\"> <br />\r\n"
                            + "        wedding_venue: <input type=\"text\" name=\"wedding_venue\"> <br/>\r\n"
                            + "        wedding id: <input type=\"number\" name=\"wedding_id\" /><br/>\r\n"
                            + "                  <input type=\"submit\" value=\"Submit\" />\r\n"
                            + "   </form>"
                            );
}

}

HTML

<! DOCTYPE html>
<html>

<head></head>

<body>
<h1>Save The Date oi oi oi</h1>

<a href = "http://localhost:8080/saveTheDate/register">Register</a>
<form action="localhost:8080/wedding" method="POST">
    Wedding date: <input type="text" name="wedding_date"> <br />
    wedding_venue: <input type="text" name="wedding_venue"> <br/>
    wedding id: <input type="number" name="wedding_id" /><br/>
              <input type="submit" value="Submit" />
</form>     
</body>
</html>


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source