In this Java servlet tutorial, I will guide you how to read values of common input fields from HTML form on the server side with Java Servlet.
You know, handling form data represented in HTML page is a very common task in web development. A typical scenario is the user fills in fields of a form and submits it. The server will process the request based on the submitted data, and send response back to the client. The following picture depicts that workflow with Java servlet on the server side:
To create a form in HTML we need to use the following tags:
This form would look like this in browser:
On the server side, we need to create a Java servlet which is mapped to the URL: loginServlet, as specified in the form’s action attribute. Following is code of the servlet:
@WebServlet("/loginServlet") public class LoginServlet extends HttpServlet < protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException < // code to process the form. >>
Notice that the servlet’s URL is specified by the @WebServlet annotation before the servlet class. When the user submits the login form above, the servlet’s doPost() method will be invoked by the servlet container. Typically we will do the following tasks inside doPost() method:
For example, we can write the following code in the doPost() method to read values of form’s fields:
String username = request.getParameter("username"); String password = request.getParameter("password");
To send response back to the client, we need to obtain a writer from the response object by calling the method getWriter() of the HttpServletResponse interface:
PrintWriter writer = response.getWriter();
Then use the print() or println() method to deliver the response (in form of HTML code). For example:
String htmlRespone = ""; htmlRespone += "Here’s complete code of the servlet class to process the login form:Your username is: " + username + "
"; htmlRespone += ""; writer.println(htmlRespone);
package net.codejava.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/loginServlet") public class LoginServlet extends HttpServlet < protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException < // read form fields String username = request.getParameter("username"); String password = request.getParameter("password"); System.out.println("username: " + username); System.out.println("password: " + password); // do some processing here. // get response writer PrintWriter writer = response.getWriter(); // build HTML code String htmlRespone = ""; htmlRespone += "Here’s an example output when submitting the above login form in browser:Your username is: " + username + "
"; htmlRespone += ""; // return response writer.println(htmlRespone); > >
"; htmlRespone += "Your password is: " + password + "
So far you have got the ins and outs when handling HTML form data with Java servlet. For your reference, we provide a list of examples for handling common HTML form fields as below. Note that we use the System.out.println() statement in servlet to demo the output.
Username: Password:
String username = request.getParameter("username"); String password = request.getParameter("password"); System.out.println("username is: " + username); System.out.println("password is: " + password);
username is: admin password is: nimda
Speaking language: English French
String languages[] = request.getParameterValues("language"); if (languages != null) < System.out.println("Languages are: "); for (String lang : languages) < System.out.println("\t" + lang); >>
Languages are: english french
Code Functional Bookshop Website with Java Servlet and Hibernate framework. Full-stack Development. Job-ready Skills.
Gender: Male Female
String gender = request.getParameter("gender"); System.out.println("Gender is: " + gender);
Gender is: male
Feedback:
String feedback = request.getParameter("feedback"); System.out.println("Feed back is: " + feedback);
Feed back is: This tutorial is very helpful. Thanks a lot!
Job Category:
String jobCategory = request.getParameter("jobCat"); System.out.println("Job category is: " + jobCategory);
Job category is: science
For handling file upload on the server side with Java servlet, we recommend these tutorials:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
FormHandlingServlet.war | [Deployable WAR file] | 4 kB |
FormHandlingServlet.zip | [Eclipse project] | 11 kB |