• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Servlet » Examples » Understanding HTML Forms
Next →
← Prev

Understanding HTML Forms

By Dinesh Thakur

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

HANDLING FORM DATA USING HTTP GET REQUEST

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,

<html>
   <head>
      <title> User Registration Form </title>    
   </head>
   <body>
      <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" />
          </p>
      </form>
   </body>
</html>

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 java.io.*; 
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 SimpleServlet.java.

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

https://ecomputernotes.com:8080/servletApp/User_reg.html

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

             

Enter the firstname, lastname and email id and click on the Submit button. As a result, SimpleServlet.java 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 & [email protected]

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 SimpleServlet.java 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,

               

You’ll also like:

  1. Anthony’s Framework for Understanding MIS
  2. How to Create a Web Forms in C#
  3. Type of Windows Forms controls
  4. PHP Forms Processing with $ _GET and $ _POST Methods
  5. What is HTML?
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Servlet Tutorials

Servlet Tutorials

  • Servlet - Home
  • Servlet - Types
  • Servlet - Advantages
  • Servlet - Container
  • Servlet - API
  • Servlet - Chaining
  • Servlet - Life Cycle
  • Servlet - Developement Way
  • Servlet - Servlet Vs CGI
  • Servlet - Server Side
  • Servlet - HttpServletRequest
  • Servlet - Cookies Advantages
  • Servlet - JDBC Architecture
  • Servlet - HttpServlet
  • Servlet - HttpServletResponse
  • Servlet - Web Technology
  • Servlet - Applet Communication
  • Servlet - Html Communication
  • Servlet - HTTP POST Request
  • Servlet - HTTP GET Request

Other Links

  • Servlet - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW