2.4 Handling HTTP POST Requests

In previous sections, we have seen servlets handle get request by overriding doGet method. The browser generates GET request :

  • when the user enters a URL on the address line
  • follows a link from a webpage
  • submits an HTML form that does not specifie method parameter for the form tag or explicitly specifies the GET method.

You've also seen that the query string is sent in the URL of a GET request.

If you need to send a password (or any sensitive piece of data), never use the GET method or you risk displaying it in the URL bar.

IF you can send data using POST request by explicitly specifying method parameter for the form tag like this :

<form method="POST" action="">
  • Query string will not be dispalyed on URL bar with a POST request.
  • If a form is sent using POST method, the data is appended to the body of the HTTP request.
  • If you need to send a large amount of data, the POST method is preferred because some browsers limit the sizes of URLs. In addition, many servers limit the length of URLs they accept.

In the following example, we will create a html form that will sent data to servlet with POST request.

user entry form

Html code of above form is:

<html>
<head>
    <title>User Entry Form</title>
</head>
<body>
    <form method="post" action="entry">
        <h3>User Entry Form</h3>
        <p>
            Username: <input type="text" name="username"> <br><br>
            Password: <input type="password" name="user-password"> <br><br>
			
            Gender: <input type="radio" name="sex" value="male">Male 
            <input type="radio" name="sex" value="female"> Female <br><br>
				
            Hobbies: 
            <input type="checkbox" name="soccer"> Soccer
            <input type="checkbox" name="cricket"> Cricket
            <input type="checkbox" name="baseball"> Baseball<br><br>
			
            Address: <textarea rows="3" cols="30"></textarea><br><br>
			
            Select Your City: 
            <select name="city">
                <option value="sydney">Sydney</option>
                <option value="melbourne">Melbourne</option>
                <option value="cromwell">Cromwell</option>
            </select><br><br>
			
            <input type="submit" value="Submit">
            <input type="reset" value="Reset">
        </p>
    </form>
</body>
</html>

Form data is sent using METHOD="POST" request. To handle post requst we override the doPost method in servlet.

package com.beginwithjava.servlet;

import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;

@WebServlet("/entry")
public class EntryForm extends HttpServlet
{
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response)
            throws ServletException, IOException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html><head><title>Add</title></head><body>");

        out.println("<p>Username: "
                + request.getParameter("username") + "</p>");
        out.println("<p>Password: "
                + request.getParameter("user-password")
                + "</p>");
        out.println("<p>Gender: "
                + request.getParameter("sex") + "</p>");
        out.println("<p>Hobbies:</p>");
        String[] sports = request
                .getParameterValues("sports");
        out.println("<ul>");
        for (String sport : sports)
        {
            out.println("<li>" + sport + "</li>");
        }
        out.println("</ul>");

        out.println("<p>Address: "
                + request.getParameter("address") + "</p>");
        out.println("<p>City: "
                + request.getParameter("city") + "</p>");
        out.println("</body></html>");
    }
}

Notice that no query string is displayed on address bar.

dopost output