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:

 form submit workflow

To create a form in HTML we need to use the following tags:

To make the form works with Java servlet, we need to specify the following attributes for the <form> tag:

For example, following is HTML code of a login form:

<form name="loginForm" method="post" action="loginServlet">
	Username: <input type="text" name="username"/> <br/>
	Password: <input type="password" name="password"/> <br/>
	<input type="submit" value="Login" />
</form>
This form would look like this in browser:

Login Form example

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:

To read values of form’s fields, the HttpServletRequest interface provides the following methods:

Note that the above methods can also deal with parameters in URL’s query string, hence the name getParameter.

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 = "<html>";
htmlRespone += "<h2>Your username is: " + username + "</h2>";
htmlRespone += "</html>";

writer.println(htmlRespone);
Here’s complete code of the servlet class to process the login form:

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 = "<html>";
		htmlRespone += "<h2>Your username is: " + username + "<br/>";		
		htmlRespone += "Your password is: " + password + "</h2>";		
		htmlRespone += "</html>";
		
		// return response
		writer.println(htmlRespone);
		
	}

}
Here’s an example output when submitting the above login form in browser:

servlet response

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.

 

1. Read values of text field and password field

username is: admin
password is: nimda

 

2. Read value of checkbox field


Java Servlet, JSP and Hibernate: Build eCommerce Website

Code Functional Bookshop Website with Java Servlet and Hibernate framework. Full-stack Development. Job-ready Skills. 

 

3. Read value of radio button field

Gender is: male

 

4. Read value of text area field

Feed back is: This tutorial is very helpful. Thanks a lot!


5. Read value of dropdown list (combobox) field

Job category is: science 

 

6. Read data of file upload field

To create a form to upload file, we need to specify the enctypeattribute for the <form> tag as follow:

<form method="post" action="uploadServlet" enctype="multipart/form-data">

		Select file to upload: <input type="file" name="uploadFile" />

		<input type="submit" value="Upload" />
</form>
For handling file upload on the server side with Java servlet, we recommend these tutorials:

For the examples in this tutorial, you can download Eclipse-based project as well as deployable WAR file under the attachments section.

 

 

Other Java Servlet Tutorials:

 


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Attachments:
Download this file (FormHandlingServlet.war)FormHandlingServlet.war[Deployable WAR file]4 kB
Download this file (FormHandlingServlet.zip)FormHandlingServlet.zip[Eclipse project]11 kB