Servlet Request Handling: doGet() vs doPost()
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(): This method is used for retrieving data from the server.
- doPost(): This method is used for sending data to the server.
Difference Between doGet() and doPost()
The below difference table lists all the major differences between doGet() and doPost():
doGet() | doPost() |
---|---|
We can use the GET method to get information from the server. | We can use the POST method to send data to the server. |
End user provided Information will be append to the URL as the part of query string and send to the server. | End user provided information will be encapsulated in the request body and send to the server. |
It is used process only small amounts of data. | It is used to handle large amount of data. |
It is basically used to retrieve data to the server. | It is basically used to sending data to the server. |
We have discussed the core differences, now let's take a closer look at each component. Let us discuss them in brief first.
doGet()
The doGet() method is a part of httpServlet class. and it is used for retrieving data from the server. And doGet() methd 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.
public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletExc eption,IOException{
PrintWriter out = resp.getWriter();
String mess = req.getParameter("mess");
out.println(mess);
}
doPost()
The doPost() method is a HttpServlet class method and it is used to send data from the client to the server. 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.
@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:

Steps 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.
<!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:
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("admin@gmail.com") && 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");
}
}
- 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.
