[ Team LiB ] Previous Section Next Section

Recipe 7.5 Posting Data from a Servlet


You want to send parameters and their values as a POST request from a servlet.


Use the Jakarta Commons HttpClient component and its PostMethod class to automate the posting of data to other programs.


The Jakarta Commons HttpClient is a component that allows the developer to mimic the features of a web browser in his Java code, such as sending GET and POST HTTP requests, as well as using HTTPS for secure sockets. As the homepage describes this useful component, HttpClient "provides an efficient, up-to-date, and feature-rich package implementing the client side of the most recent HTTP standards and recommendations" (http://jakarta.apache.org/commons/httpclient/). HttpClient is offered under the Apache Software License.

This recipe describes using HttpClient to post data to another server-side program using the POST HTTP method. First, download the HttpClient distribution from the Jakarta site (http://jakarta.apache.org/commons/httpclient/downloads.html). Then unpack the distribution and place the JAR file that it contains in the WEB-INF/lib directory of your web application. At this writing, the JAR for Release 2.0 Alpha 3 was commons-httpclient-2.0-alpha2.jar. Once you have taken care of this installation, your servlets and beans can use the HttpClient classes.

Example 7-9 is a servlet that posts data to a JSP: http://localhost:8080/home/viewPost.jsp. Example 7-3 shows the viewPost.jsp file. Note the classes from the org.apache.commons.httpclient package that the servlet has to import at the top of the code.

Example 7-9. A servlet that posts data to a JSP using HttpClient
package com.jspservletcookbook;

import javax.servlet.*;
import javax.servlet.http.*;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.NameValuePair;

public class ClientPost extends HttpServlet {
 public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, java.io.IOException {
    HttpClient httpClient = new HttpClient( );

    PostMethod postMethod = new PostMethod(

    NameValuePair[] postData = {
          new NameValuePair("username", "devgal"),
          new NameValuePair("department", "development"),
          new NameValuePair("email", "devgal@yahoo.com")

   //the 2.0 beta1 version has a
   //method, as addParameters is deprecated



    //display the response to the POST method
    java.io.PrintWriter out = response.getWriter( );
    //A "200 OK" HTTP Status Code

    if (postMethod.getStatusCode( ) == HttpStatus.SC_OK) {

        out.println(postMethod.getResponseBodyAsString( ));

    } else {

        out.println("The POST action raised an error: " + postMethod.getStatusLine( ));

        //release the connection used by the method
    postMethod.releaseConnection( );
  public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, java.io.IOException {


The code sends three name/value pairs to the JSP (named username, department, and email), which will handle the posted data. HttpClient handles the returned text from the POST method so that you can display it in the same servlet. If you expect to receive large amounts of text as return values from the POST, then consider using the HttpMethodBase.getResponseBodyAsStream( ) method instead of getResponseBodyAsString( ). The getResponseBodyAsStream( ) method returns a java.io.InputStream. Example 7-9 is derived from sample code provided at the HttpClient web site.

Figure 7-4 shows the web browser display after requesting the ClientPost servlet.

Figure 7-4. Displaying the returned text after posting data from a servlet

See Also

Recipe 7.1 on handling a POST request in a servlet; Recipe 7.7 on using a servlet to add a parameter to a query string; the Jakarta Commons HttpClient page: http://jakarta.apache.org/commons/httpclient.

    [ Team LiB ] Previous Section Next Section