HttpServlet doPost() Method Example

In this post, we will demonstrate the usage of the HttpServlet class provided doPost() method with an example. The doPost() method is called by the server (via the service method) to allow a servlet to handle a POST request. Generally, we use the doPost() method for sending information to the server like HTML form data.

Add servlet dependency to pom.xml or classpath

Let's add servlet 4.0.1 dependency to pom.xml:
        <!-- -->
If your project is not a maven project then you can download the jar file and keep in the classpath.

Handling HTTP POST Request Example

Here we will develop a servlet that handles an HTTP POST request. Let's create a student.jsp file which contains a Student registration HTML form code:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html>
<meta charset="ISO-8859-1">
<title>JSP Actions Example</title>

<h1> Student Registration Page</h1>
 <form action="<%= request.getContextPath() %>/StudentServlet" method="post">
  First Name: <input type="text" name="firstName">
  <br> <br> 
  Last Name: <input type="text" name="lastName">
  <br> <br> 
  Email ID: <input type="email" name="emailId">
  <br> <br> 
  Password: <input type="password" name="password"><br>
  <input type="submit" value="register">
Notice that the action parameter of the form tag specifies a URL. The URL identifies a servlet to process the HTTP POST request.
Let's run this example in tomcat server and hit http://localhost:8080/java-servlet-tutorial/student.jsplink into a browser will result below web page: 
Let's create file and enter the following code into it. The doPost( ) method is overridden to process any HTTP POST requests that are sent to this servlet. It uses the getParameter( ) method of HttpServletRequest to obtain the selection that was made by the user. A response is then formulated.
package net.javaguides.servlet.tutorial.examples;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class StudentServlet extends HttpServlet {

    private static final long serialVersionUID = 1 L;

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String firstName = req.getParameter("firstName");
        String lastName = req.getParameter("lastName");
        String emailId = req.getParameter("emailId");
        String password = req.getParameter("password");

        PrintWriter printWriter = resp.getWriter();
        printWriter.print("<h1>Student Resistration Form Data</h1>");
        printWriter.print("<p> firstName :: " + firstName + "</p>");
        printWriter.print("<p> lastName :: " + firstName + "</p>");
        printWriter.print("<p> firstName :: " + firstName + "</p>");
        printWriter.print("<p> firstName :: " + firstName + "</p>");

        System.out.println("firstName :: " + firstName);
        System.out.println("lastName :: " + lastName);
        System.out.println("emailId :: " + emailId);
        System.out.println("password :: " + password);
Let's fill above Student registration form and hit submit button will result below page: