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:
- Java Development Kit (JDK) is installed on your system.
- Apache Tomcat server (version 9 or higher).
- IDE like Eclipse, IntelliJ IDEA, or NetBeans.
- Basic knowledge of Java and HTML forms.
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
- Create a Dynamic Web Project in your IDE.
- 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:
<!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.
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
<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
- Deploy the project to Apache Tomcat.
- Access
index.htmlin your browser. - Enter two numbers and submit the form.
Output:

Step 3: Handling POST Requests
To send sensitive data securely, switch the form method to POST.
HTML Form for POST Request
<!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:
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
- Redeploy and restart the server.
- Open the updated HTML form in the browser.
- Enter two numbers and submit.
Behavior:
- Parameters are hidden in the request body.
- Output is displayed in the browser:

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

This ensures both GET and POST requests are handled by a single method, improving code readability and maintainability.
Choosing Between GET and POST
| Criteria | GET | POST |
|---|---|---|
| Data Visibility | Parameters visible in URL | Parameters hidden in request body |
| Use Case | Retrieving or reading data | Submitting or updating data |
| Caching | Supported by browsers | Not cached |
| Security | Less secure | More secure |
| Idempotent | Yes | No |