FileDocCategorySizeDatePackage
PostBean.javaAPI DocExample2523Tue Feb 10 07:37:42 GMT 2004com.jspservletcookbook

PostBean

public class PostBean extends Object implements Serializable

Fields Summary
private Map
parameters
private String
url
Constructors Summary
public PostBean()

Methods Summary
public java.util.MapgetParameters()


    return parameters;
    
private org.apache.commons.httpclient.NameValuePair[]getParams(java.util.Map map)

 
          NameValuePair[] pairs = new NameValuePair[map.size()];
          //Use an Iterator to put name/value pairs from the Map 
            //into the array
          Iterator iter = map.entrySet().iterator();
          int i = 0;
          while (iter.hasNext()){
             
            Map.Entry me = (Map.Entry) iter.next();
             pairs[i] = new NameValuePair(
                       (String)me.getKey(),((String[]) me.getValue())[0]);
            i++;
          }
          return pairs;
 
public java.lang.StringgetPost()


    if (url == null || url.equals("") || parameters == null)
        throw new IllegalStateException("Invalid url or parameters in PostBean.getPost method.");

    String returnData = "";
    HttpClient httpClient = new HttpClient();
    PostMethod postMethod = new PostMethod(url);
    //convert the Map passed into the bean to a NameValuePair[] type
    NameValuePair[] postData = getParams(parameters);  
    //the 2.0 beta1 version has a
   //PostMethod.setRequestBody(NameValuePair[])
    //method, as addParameters is deprecated
    postMethod.addParameters(postData);
    httpClient.executeMethod(postMethod);
     //A "200 OK" HTTP Status Code
    if (postMethod.getStatusCode() == HttpStatus.SC_OK) {
        returnData= postMethod.getResponseBodyAsString();
    } else {
        returnData= "The POST action raised an error: " + postMethod.getStatusLine();
    }
    //release the connection used by the method
    postMethod.releaseConnection();
    return returnData;
  
public java.lang.StringgetUrl()


    return url;
    
public voidsetParameters(java.util.Map param)


  if (param != null)
      parameters = param;
public voidsetUrl(java.lang.String url)


  if (url != null && !(url.equals("")))
      this.url=url;