Sunday, 16 March 2014

Servlet – Post method with example

In this tutorial we are going to see about Servlet Post method with example program.
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // code block

The doPost method called by the server (via the service method) to allow a servlet to handle a POST request. The HTTP POST method allows the client to send data of unlimited length to the Web server a single time and is useful when posting information such as credit card numbers.

When overriding this method, read the request data, write the response headers, get the response's writer or output stream object, and finally, write the response data. It's best to include content type and encoding. When using a PrintWriter object to return the response, set the content type before accessing the PrintWriter object.

The servlet container must write the headers before committing the response, because in HTTP the headers must be sent before the response body.

Where possible, set the Content-Length header (with the ServletResponse.setContentLength(int) method), to allow the servlet container to use a persistent connection to return its response to the client, improving performance. The content length is automatically set if the entire response fits inside the response buffer.

When using HTTP 1.1 chunked encoding (which means that the response has a Transfer-Encoding header), do not set the Content-Length header.

This method does not need to be either safe or idempotent. Operations requested through POST can have side effects for which the user can be held accountable, for example, updating stored data or buying items online.

If the HTTP POST request is incorrectly formatted, doPost returns an HTTP "Bad Request" message.

Now see how to submit form using Servlet post method.

1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project .

2. Create the Project called ServletExample as given below.

Servlet-dynamic web project

3. Create package called com.javatutorialscorner.servlet under ServletExample.

4. Create Servlet called ServletPostExample as shown in figure.

Create Servlet

http servlet

5. Click Next it will show URL mapping.You can edit Servlet URL if you need.

6. Click Next it will show methods available  in HttpServlet. Select appropriate method you need.

http servlet Services

7. By default your servlet will be mapped in web.xml, if your servlet not mapped in your web.xml use the following configuration to map the servlet in your web.xml

Servlet Mapping

    <servlet-name>Your Servlet Name</servlet-name>
    <servlet-class>Fully Qulaified Servlet Class Name</servlet-class>
    <servlet-name>Your Servlet Name</servlet-name>
    <url-pattern>/URL to Call Servlet</url-pattern>


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="3.0">


8. Add the required code inside doPost() method.

package com.javatutorialscorner.servlet;


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

 * Servlet implementation class ServletPostExample

public class ServletPostExample extends HttpServlet {
 private static final long serialVersionUID = 1L;
     * @see HttpServlet#HttpServlet()
    public ServletPostExample() {
        // TODO Auto-generated constructor stub

  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub

  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
  String password = request.getParameter("password");
  PrintWriter writer = response.getWriter();
  writer.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"\">\n"
    + "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\"><title>Java Tutorials Corner - Servlet POST method</title>"
    + "</head><body><h1>Java Tutorials Corner - Servlet POST method</h1><table><tr>"
    + "<td>Name : </td><td>"
    + request.getParameter("name")
    + "</td></tr><tr><td>Job : </td><td>"
    + request.getParameter("job") + "</td></tr></table></body></html>");


9. Create html page in WebContent folder


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Java Tutorials Corner - Servlet Form Submit using POST method</title>
<h1>Java Tutorials Corner - Servlet Form Submit using POST method</h1>
<form action="ServletPostExample" method="post">

<td>Name : </td>
<td><input type="text" name="name"/> </td>
<td>Job : </td>
<td><input type="text" name="job"/></td>
<td>Password : </td>
<td><input type="password" name="password"/></td>
<input type="submit" value="Submit"/>

In above html page in form tag method="post" to mention the form submit using post method.The default method to submit form is GET.

10. Now save and Run the servlet, Run As –> Run on Server – Select your web Server to run the servlet. (see How to configure tomcat in eclipse the following URL .



servlet submit from

After form Submitted

parameter in the form not show in URL of browser

servlet form submit output


Post a Comment