Handling HTTP GET and POST Requests in Servlets

Last Updated : 8 Nov, 2025

Handling HTTP GET and POST requests in Java Servlets is essential for building dynamic and interactive web applications. Servlets provide a powerful way to process client requests, send responses, and maintain control over how data is exchanged between the client and server.

This article explains how to handle GET and POST requests in servlets, their key differences, implementation steps, and best practices for writing maintainable code.

Prerequisites

Before implementing servlet request handling, ensure the following setup:

Understanding HTTP Methods in Servlets

HTTP defines several methods for communication such as GET, POST, PUT, and DELETE. Among them, GET and POST are most commonly used in servlet-based applications.

GET Method

  • Used to request data from the server.
  • Parameters are appended to the URL and are visible.
  • Suitable for retrieving non-sensitive data such as search queries.

POST Method

  • Used to send data to the server.
  • Parameters are sent in the request body, not visible in the URL.
  • Suitable for sending sensitive or form data and modifying server-side resources.

Note: If the method attribute is not specified in an HTML form, it defaults to GET.

Implementation Steps

Step 1: Setting Up the Project

  1. Create a Dynamic Web Project in your IDE.
  2. Ensure the presence of a web.xml file in the WEB-INF folder for servlet configuration (if not using annotations).

Step 2: Handling GET Requests

HTML Form for GET Request

Create an HTML form to send data to the server using the GET method. Save the file as index.html:

HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>GET Request Example</title>
</head>
<body>
  <form action="add" method="get">
    Enter num1: <input type="text" name="num1"><br>
    Enter num2: <input type="text" name="num2"><br>
    <input type="submit" value="Add">
  </form>
</body>
</html>

Servlet to Handle GET Request

Create a servlet class named AddServlet to handle GET requests.

Java
package com.learnservlets;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AddServlet extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
        int num1 = Integer.parseInt(req.getParameter("num1"));
        int num2 = Integer.parseInt(req.getParameter("num2"));
        int result = num1 + num2;

        PrintWriter out = res.getWriter();
        out.println("The result is = " + result);
    }
}

Servlet Mapping in web.xml

XML
<web-app>
    <servlet>
        <servlet-name>AddServlet</servlet-name>
        <servlet-class>com.learnservlets.AddServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>AddServlet</servlet-name>
        <url-pattern>/add</url-pattern>
    </servlet-mapping>
</web-app>

Testing the GET Request

  1. Deploy the project to Apache Tomcat.
  2. Access index.html in your browser.
  3. Enter two numbers and submit the form.

Output:

executing-getMethod
Handling GET Requests

Step 3: Handling POST Requests

To send sensitive data securely, switch the form method to POST.

HTML Form for POST Request

HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>POST Request Example</title>
</head>
<body>
  <form action="add" method="post">
    Enter num1: <input type="text" name="num1"><br>
    Enter num2: <input type="text" name="num2"><br>
    <input type="submit" value="Add">
  </form>
</body>
</html>


If the servlet only has a doGet() method, submitting this form will return a 405 HTTP Method Not Allowed error because the servlet does not handle POST requests yet.

Updated Servlet to Handle POST

Add the doPost() method to process POST requests:

Java
package com.learnservlets;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AddServlet extends HttpServlet {
    public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {
        int num1 = Integer.parseInt(req.getParameter("num1"));
        int num2 = Integer.parseInt(req.getParameter("num2"));
        int result = num1 + num2;

        PrintWriter out = res.getWriter();
        out.println("The result is = " + result);
    }
}

Testing the POST Request

  1. Redeploy and restart the server.
  2. Open the updated HTML form in the browser.
  3. Enter two numbers and submit.

Behavior:

  • Parameters are hidden in the request body.
  • Output is displayed in the browser:
Output
Handling POST request

You can also test the POST method using tools like Postman.

Step 4: Handling Both GET and POST Requests

To avoid code duplication, define a shared method for the core logic and invoke it from both doGet() and doPost().

Java
package com.learnservlets;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AddServlet extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
        processRequest(req, res);
    }

    public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {
        processRequest(req, res);
    }

    private void processRequest(HttpServletRequest req, HttpServletResponse res) throws IOException {
        int num1 = Integer.parseInt(req.getParameter("num1"));
        int num2 = Integer.parseInt(req.getParameter("num2"));
        int result = num1 + num2;

        PrintWriter out = res.getWriter();
        out.println("The result is = " + result);
    }
}

Output:

BothGetandPost
Handling both GET and POST requests

This ensures both GET and POST requests are handled by a single method, improving code readability and maintainability.

Choosing Between GET and POST

CriteriaGETPOST
Data VisibilityParameters visible in URLParameters hidden in request body
Use CaseRetrieving or reading dataSubmitting or updating data
CachingSupported by browsersNot cached
SecurityLess secureMore secure
IdempotentYesNo
Comment

Explore