I have a JSP page with an HTML form:
<form action="SERVLET"> <input type="text" name="name"/><br> <input type="text" name="group"/> <input type="text" name="pass"/> <input type="submit" value="submit"> </form>
How to obtain these data in a servlet and add them to database?
JSP handles form data parsing automatically using the following methods depending on the situation: getParameter() − You call request. getParameter() method to get the value of a form parameter.
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 <form action>
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 (<input>
, <select>
, <textarea>
and <button>
). 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:
<form action="${pageContext.request.contextPath}/yourServletURL" method="post"> <p>Normal text field. <input type="text" name="name" /></p> <p>Secret text field. <input type="password" name="pass" /></p> <p>Single-selection radiobuttons. <input type="radio" name="gender" value="M" /> Male <input type="radio" name="gender" value="F" /> Female</p> <p>Single-selection checkbox. <input type="checkbox" name="agree" /> Agree?</p> <p>Multi-selection checkboxes. <input type="checkbox" name="role" value="USER" /> User <input type="checkbox" name="role" value="ADMIN" /> Admin</p> <p>Single-selection dropdown. <select name="countryCode"> <option value="NL">Netherlands</option> <option value="US">United States</option> </select></p> <p>Multi-selection listbox. <select name="animalId" multiple="true" size="2"> <option value="1">Cat</option> <option value="2">Dog</option> </select></p> <p>Text area. <textarea name="message"></textarea></p> <p>Submit button. <input type="submit" name="submit" value="submit" /></p> </form>
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);
Well, there are plenty of database tutorials online for java (what you're looking for is called JDBC). But if you are using plain servlets, you will have a class that extends HttpServlet
and inside it you will have two methods that look like
public void doPost(HttpServletRequest req, HttpServletResponse resp){ }
and
public void doGet(HttpServletRequest req, HttpServletResponse resp){ }
One of them is called to handle GET
operations and another is used to handle POST
operations. You will then use the HttpServletRequest
object to get the parameters that were passed as part of the form like so:
String name = req.getParameter("name");
Then, once you have the data from the form, it's relatively easy to add it to a database using a JDBC tutorial that is widely available on the web. I also suggest searching for a basic Java servlet tutorial to get you started. It's very easy, although there are a number of steps that need to be configured correctly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With