VOOZH about

URL: https://www.geeksforgeeks.org/advance-java/handling-http-get-and-post-requests-in-servlets/

⇱ Handling HTTP GET and POST Requests in Servlets - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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:

Servlet to Handle GET Request

Create a servlet class named AddServlet to handle GET requests.

Servlet Mapping in web.xml

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


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:

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().

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
Article Tags:

Explore