Servlet Request Handling: doGet() vs doPost()
Last Updated :
22 Jul, 2025
In Servlet doGet() and doPost(), both are methods of the HttpServlet class. These methods are used to handle client requests, proceed with those requests, and generate corresponding responses. The main difference between doGet() and doPost() is:
- doGet() method is used for retrieving data from the server.
- doPost() method is used for sending data to the server.
Difference Between doGet() and doPost()
doGet() method does not provide security because the values are shown in the URL bar. By using GET request we can send only Character Data (ASCII) and we cannot send Binary Data like Images.
- It is less secure for sensitive data .
- It is used for Displaying data.
- Use small amount of data.
Java
public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletExc eption,IOException{
PrintWriter out = resp.getWriter();
String mess = req.getParameter("mess");
out.println(mess);
}
doPost() method provide more security as compare to doGet(). By using POST request we can send both Binary and Character Data to the Server.
- It is more secure for sensitive data.
- POST requires more server-side processing, so it's slightly slower
- Use large amount of data.
Java
@Override
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws Exception {
String username = req.getParameter("username");
String password = req.getParameter("password");
response.setContentType("text/html");
PrintWriter out = res.getWriter();
if (username.equals("vishnu") && password.equals("vishnu123")) {
out.println("<h3>Welcome to user page</h3>");
System.out.println("welcome");
} else {
out.println("<h2>Please enter correct details</h2>");
System.out.println("Login failed");
}
}
Example:
We will create a simple form to get the details from user like below:
user formSteps to Implement
- Create an HTML form to take the input data from the user.
- Create a Servlet for handling http get and post request.
Create User.html page:
- It is a simple HTML login form that allows a user to enter their details according to field..
- Create a form that sends data to the servlet named
"myLogin"
using post request.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Servlet Login Form</title>
</head>
<body>
<h3>Login Form</h3>
<form action="myLogin" method="post">
<label>User Email:</label><br>
<input type="text" name="username" placeholder="Enter Email" required><br><br>
<label> Enter password:</label><br>
<input type="password" name="password" placeholder="Enter Password" required><br><br>
<input type="submit" value="Login">
</form>
</body>
</html>
Create MyServlet.java Servlet:
Java
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@WebServlet("/myLogin")
public class MyServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String email = request.getParameter("username");
String password = request.getParameter("password");
if (email.equals("[email protected]") && password.equals("admin123")) {
out.print("<html><body>");
out.print("<h2>login successfull </h2>");
out.print("Email " + email + "<br/>");
out.print("Password: " + password + "<br/>");
out.print("</body></html>");
} else {
out.print("<html><body>");
out.print("<h3>failed</h3>");
out.print("<p>Enter correct user details.</p>");
out.print("</body></html>");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.sendRedirect("User.html");
}
}
Explanation:
- Here, we have used the @WebServlet annotation to map URL requests to the servlet.
- When the user clicks Login, the form data is sent to the server.
- Once the form page is submitted, the 'doPost()' method of the Servlet class will be invoked.
- doPost() method is called automatically when the server receives a POST request because in html page method type is post.
- Inside
doPost
, method read the input using request.getparameter() and this method match the attribute in the form. - And then compare the credentials provided by you.
- and then generate the response according to our credentials
- Suppose if we provide the user email and password then we have to show a message on the browser
Output:
- To Run the application, right-click on the project, Run As -> Run on Server
- Run the URL: http://localhost:8080/myServlet/User.html in the browser to get the Initial form page.
Output