I have a JSP page with an HTML form:
Create a class which extends HttpServlet and put @WebServlet annotation on it containing the desired URL the servlet should listen on.
@WebServlet("/yourServletURL")
public class YourServlet extends HttpServlet {}
And just let point to this URL. I would also recommend to use POST method for non-idempotent requests. You should make sure that you have specified the
name
attribute of the HTML form input fields (,
,
and
). This represents the HTTP request parameter name. Finally, you also need to make sure that the input fields of interest are enclosed inside the desired form and thus not outside.
Here are some examples of various HTML form input fields:
Create a doPost() method in your servlet which grabs the submitted input values as request parameters keyed by the input field's name
(not id
!). You can use request.getParameter()
to get submitted value from single-value fields and request.getParameterValues()
to get submitted values from multi-value fields.
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String pass = request.getParameter("pass");
String gender = request.getParameter("gender");
boolean agree = request.getParameter("agree") != null;
String[] roles = request.getParameterValues("role");
String countryCode = request.getParameter("countryCode");
String[] animalIds = request.getParameterValues("animalId");
String message = request.getParameter("message");
boolean submitButtonPressed = request.getParameter("submit") != null;
// ...
}
Do if necessary some validation and finally persist it in the DB the usual JDBC/DAO way.
User user = new User(name, pass, roles);
userDAO.save(user);