by Dinesh Thakur Category: Basic Servlet Examples

Before handling HTTP get requests and HTTP post requests, you should have some knowledge about HTML forms.

HTML forms provide a simple and reliable user interface to collect data from the user and transmit the data to a servlet or other server side programs for processing. You might have seen HTML forms while using search engine visiting online book stores, tracking stocks on the web, getting information about availability of train tickets etc.

In order to construct a HTML form the following HTML tags are generally used .

<FORM ACTION = "url" METHOD = "method"></FORM>

This tag defines the form body. It contains two attributes action and method. The action attribute specifies the address of the server program (i.e. servlet or JSP page) that will process the form data when the form is submitted. The method attribute can be either GET or POST, it indicates the type of HTTP request sent to the server. If the Method attribute is set to GET, the form data will be appended to the end of the specified URL after the question mark. If it is set to POST, then the form data will be sent after the HTTP request headers and a blank line.

•  <INPUT TYPE = "type" NAME = "name" >............ </INPUT>

This tag creates an input field. The type attribute specifies the input type. Possible types are text for single line text field, radio for radio button, checkbox for a check box ,textarea for multiple line text field, password for password field, submit for submit button ,reset for reset button etc. The name attribute gives a formal name for the attribute. This name attribute is used by the servlet program to retrieve its associated value. The names of radio buttons in a group must be identical.

<SELECT NAME = "name" SIZE =" size">…….. </SELECT>

This tag defines a combobox or a list. The NAME attribute gives it a formal name. The SIZE attribute specifies the number of rows in the list.

<OPTION SELECTED= "selected" VALUE = "value">

This tag defines a list of choices within the <SELECT> and </SELECT> tag. The value attribute gives the value to be transmitted with the name of the select menu if the current position is selected. The selected attribute specifies the particular menu item shown is selected, when the page is loaded.

           user registration form

                 The Fig. shows a user registration form


In order to handle form data using HTTP GET request, we first create a user registration form as shown earlier and a servlet that will handle HTTP GET request. The servlet is invoked when the user enters the data in the form and clicks the submit button. The servlet obtains all the information entered by the user in the form and displays it.

The coding for user-registration form (c:\Tomcat6\webApps\examples) is as follows,

      <title> User Registration Form </title>    
      <h2 align=”center”> User Registration Form </h2>
      <form method =”get” action =”/examples/servlet/DisplayUserInfo”>
       <p align=”center”> First Name :<input type=”text” name=”fname”/>                              
          <p align=”center”> Last Name : <input type=”text” name=”lname”/>
          <p align=”center”> Email Id : <input type=”text” name=”emailid”/>
          <p align=”center”> <input type="submit" value="submit" />
          <input type="reset" value="Reset" />

In the given html code, notice that the Method attribute of the FORM tag is set to GET, which indicates that the HTTP GET request is sent to the server. The ACTION attribute of the FORM tag is set to/examples/servlet/DisplayUserInfo which identifies the address of the servlet that will process the HTTP GET request. The code for the DisplayUserInfo. java is as follows,.


import javax.servlet.*;
import javax.servlet.http.*;
public class DisplayUserInfo extends HttpServlet
     public void doGet (HttpServletRequest request, HttpServletResponse
     response) throws ServletException, IOException
          response.setContentType (“text/html”);
          PrintWriter out =response.getWriter ();   
          String fname = request.getParameter (“fname”);
          String lname = request.getParameter (“lname”);
          String emailid = request.getParameter (“emailid”);
          out.println (“You have entered:<br>”)
          out.println (“First Name ……”+fname);
          out.println (“<br> Last Name ……”+lname);
          out.println (“<br> Emailid ……”+emailid);
          out.println (“<h2> thanks for registration </h2> <hr>”);
          out.close ();

In the above servlet code, our servlet class DisplayUserInfo extends HttpServlet class which is the most likely class that all the servlets will extend when you create servlets in web application. We override doGet () method as it has to process HTTP GET request. This method takes an HttpServletRequest object that encapsulates the information contained in the request and HTTPServletResponse object that encapsulates the information contained in the response. Our implementation of doGet () method performs two tasks:

• To extract the form parameters from HTTP request.

• To generate the response.

We call the getParameter () method of HTTP servlet request to get the value of a form parameter. If the parameter does not exist, this method returns null. It returns an empty string if an empty value is passed to the servlet. The rest of the statements in the servlet are similar to the ones we have already discussed in

NOTE: The parameter name specified in the getParameter () method must match to the one that is specified in HTML source code. Also, the parameter names are case sensitive.

Now compile and save the servlet program in the C:\Tomcat6\webapps\examples\WEB-INF\classes directory. After this, open your web browser and type the following URL


To load user_reg. html in the web browser. As a result, the following webpage will be displayed.

             Understanding HTML Forms

Enter the firstname, lastname and email id and click on the Submit button. As a result, servlet will be invoked. When you click the Submit but ton, the values you entered in the form are passed as name-value pars in a GET request. This form data will be appended to the end of the specified URL after the question mark as shown

Address: http:localhost:8080/examples/servlet/DisplayUserInfo? fname =Daljeet & lname=Singh & emailid= This email address is being protected from spambots. You need JavaScript enabled to view it.

it is clear that name-value pairs are passed with the name and value separated by ‘=’. If there is more than one name-value pair, each pair is separated by ampersand (&).

When the servlet receives the request, it extracts the fname, lname and email parameters from the HTTP request using the getParameter () method of the HttpServletRequest interface. The statement,

String fname = request.getParameter (“fname”);

will extract the parameter named fname in the <FORM> tag of the user-reg.html file and store it in request object. Similarly, we extract the lname and email parameters.

After processing the request, the servlet displays the form data along with an appropriate message.

When the user enter the data in the form displayed in the web browser and click the Submit button, then as in case of get () method, the servlet obtains all the information entered by the user in the form and displays it. But unlike the get () method, the values entered are not appended to the requested URL rather the form data will be sent after the HTTP request headers and a blank line.

The output displayed will be as follows,

               Understanding HTML Forms

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.