Servlet - Form Data

Last Updated : 11 May, 2026

Servlets are Java programs used to handle client requests and generate dynamic responses in web applications. They run on the server side and help process HTML form data submitted by users through web browsers. Servlets receive the request from the client, process the data, and send the appropriate response back to the browser.

  • Execute on the server side to process client requests.
  • Handle form data and HTTP requests efficiently.
  • Generate dynamic responses for web applications.

Flow Hierarchy of Form Data in Servlet

The following diagram illustrates how HTML form data is sent from the browser to the servlet, processed on the server, and returned back to the client as a response.

flow_of_form_data_in_servlet
  • User Fills the HTML Form : The user enters details into the HTML form fields and clicks the submit button.
  • Browser Sends Request to Server : The browser sends the form data to the web server using the HTTP GET or POST method.
  • Servlet Container Receives the Request : The servlet container receives the request and creates the HttpServletRequest object.
  • Servlet Processes the Request : The request is forwarded to the appropriate servlet method such as doGet() or doPost().
  • Servlet Reads the Form Data : The servlet reads the submitted form values using methods like getParameter() and getParameterValues().
  • Servlet Sends Response Back : After processing the data, the servlet generates the response and sends it back to the browser for display.

HttpServlet Class

Java provides the HttpServlet class to handle HTTP-specific requests and responses in web applications. It extends the GenericServlet class and provides methods such as doGet(), doPost(), doPut(), and doDelete() for processing different types of HTTP requests.

  • Provides methods to handle different HTTP request types.
  • Used to create HTTP-based servlet applications.

Commonly Used Methods of HttpServlet

GET Method and doGet() Method

The doGet() method of the HttpServlet class is used to process HTTP GET requests. It is mainly used to retrieve data from the server.

  • Data is sent through the URL as query parameters.
  • Suitable for non-sensitive data.
  • Data is visible in the browser URL.

URL Syntax:

http://localhost:8080/HelloServlet/hello?username=Ravi

POST Method and doPost()

The doPost() method of the HttpServlet class is used to process HTTP POST requests. It sends form data through the request body, making it more secure than the GET method.

  • Data is not visible in the browser URL.
  • Suitable for sensitive and large amounts of data.
  • Commonly used for form submissions.

URL Syntax:

http://localhost:8080/ServletProject/FormData

ServletRequest Interface

Java provides the ServletRequest interface to read and process client request data in servlets.

  • Retrieve form field values.
  • Access request parameters and attributes.
  • Read client request information.

HttpServletRequest Interface

The HttpServletRequest interface is used to access HTTP request information in servlet applications. The servlet container creates its object and passes it to methods like doGet() and doPost().

  • Reads client form data.
  • Retrieves request parameters and headers.
  • Supports HTTP request processing.

Methods Used for Form Data Handling

1. getParameter() Method

The getParameter() method is used to retrieve a single value submitted through the form.

Syntax:

String value = request.getParameter("parameterName");

2. getParameterValues() Method

The getParameterValues() method is used when multiple values are selected for the same field, such as checkboxes.

Syntax:

String values[] = request.getParameterValues("parameterName");

3. getParameterNames() Method

The getParameterNames() method is used to retrieve all parameter names submitted in the request.

Syntax:

Enumeration<String> names = request.getParameterNames();

4. getAttribute() Method

The getAttribute() method is used to retrieve the value of an attribute stored in the request object.

Syntax:

Object value = request.getAttribute("attributeName");

5. setAttribute() Method

The setAttribute() method is used to store data as an attribute in the request object.

Syntax:

request.setAttribute("attributeName", value);

Create a simple form to get the details from client

We will create a simple form to get the details from client like below,

Application Form

 Step 1: Create Details.html Page

Create an HTML form using common form elements such as text fields, radio buttons, checkboxes, dropdowns, and textarea.

HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>User Details</title>
</head>

<body>

<h3>Fill in the Form</h3>

<form action="FormData" method="post">

<table>

<tr>
<td>Full Name:</td>
<td><input type="text" name="name" /></td>
</tr>

<tr>
<td>Phone Number:</td>
<td><input type="text" name="phone" /></td>
</tr>

<tr>
<td>Gender:</td>

<td>
<input type="radio"
       name="gender"
       value="male" />Male

<input type="radio"
       name="gender"
       value="female" />Female
</td>
</tr>

<tr>
<td>Select Programming Languages:</td>

<td>
<input type="checkbox"
       name="language"
       value="java" />Java

<input type="checkbox"
       name="language"
       value="python" />Python

<input type="checkbox"
       name="language"
       value="sql" />SQL

<input type="checkbox"
       name="language"
       value="php" />PHP
</td>
</tr>

<tr>
<td>Select Course Duration:</td>

<td>
<select name="duration">

<option value="3months">
3 Months
</option>

<option value="6months">
6 Months
</option>

<option value="9months">
9 Months
</option>

</select>
</td>
</tr>

<tr>
<td>Comments:</td>

<td>
<textarea rows="5"
          cols="40"
          name="comment"></textarea>
</td>
</tr>

</table>

<input type="submit"
       value="Submit Details">

</form>

</body>
</html>

Step 2: Create FormDataHandle.java Servlet

Create a servlet class to handle the submitted form data.

Java
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("/FormData")
public class FormDataHandle extends HttpServlet {

    protected void doPost(HttpServletRequest request,
                          HttpServletResponse response)
        throws ServletException, IOException
    {

        // Retrieve single-value parameters
        String name = request.getParameter("name");

        String phNum = request.getParameter("phone");

        String gender = request.getParameter("gender");

        String courseDur = request.getParameter("duration");

        String comment = request.getParameter("comment");

        // Retrieve multiple checkbox values
        String progLang[]
            = request.getParameterValues("language");

        // Store selected languages
        String langSelect = "";

        if (progLang != null) {

            for (int i = 0; i < progLang.length; i++) {

                langSelect
                    = langSelect + progLang[i] + ", ";
            }
        }

        // Set response type
        response.setContentType("text/html");

        // Create PrintWriter object
        PrintWriter out = response.getWriter();

        // Generate response
        out.print("<html><body>");

        out.print("<h3>Details Entered</h3>");

        out.print("Full Name: " + name + "<br/>");

        out.print("Phone Number: " + phNum + "<br/>");

        out.print("Gender: " + gender + "<br/>");

        out.print("Programming Languages: " + langSelect
                  + "<br/>");

        out.print("Course Duration: " + courseDur
                  + "<br/>");

        out.print("Comments: " + comment);

        out.print("</body></html>");
    }
}

Step 3: Run the Application

Follow below steps to Run

  1. Right-click the project.
  2. Select Run As -> Run on Server.
  3. Open the following URL in the browser:

http://localhost:8080/JavaServletFormData/Details.html

4. Enter the form details.
5. Click on Submit Details.

Output:

Details.html page

The HTML form page will be displayed with all the fields.

Enter all the values in respective fields and click on 'Submit Details'. The details will be submitted to the servlet and the data is written to the response that displays on the browser.

Output details

Now you can observe the URL mapped to the servlet after form submission

Comment