Client Post : Client « Servlets « Java

Client Post

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, {

    HttpClient httpClient = new HttpClient();
    PostMethod postMethod = new PostMethod(
    NameValuePair[] postData = { new NameValuePair("username", "devgal"),
        new NameValuePair("department", "development"),
        new NameValuePair("email", "") };
    //the 2.0 beta1 version has a
    // PostMethod.setRequestBody(NameValuePair[])
    //method, as addParameters is deprecated
    //display the response to the POST method
    response.setContentType("text/html"); out = response.getWriter();
    //A "200 OK" HTTP Status Code
    if (postMethod.getStatusCode() == HttpStatus.SC_OK) {
    } else {
      out.println("The POST action raised an error: "
          + postMethod.getStatusLine());
    //release the connection used by the method


  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, {

    doPost(request, response);


Related examples in the same category

1.Client Pull and Move
2.Client Pull
3.Get Users IP Address