Source Code : JSP post

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

JSP post

//jspPost.jsp
/*

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<c:set var="mapParams" value="${param}" />
<jsp:useBean id="postBean" class="com.java2s.PostBean" >
<jsp:setProperty name="parameters" value="${mapParams}" />
<jsp:setProperty name="url" value="http://localhost:8080/home/viewPost.jsp" />
</jsp:useBean>
<jsp:getProperty id="postBean" property="post"/>

*/

//viewPost.jsp
/*
<%@page contentType="text/html"%><%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
<head><title>Post Data Viewer</title></head>
<body>
<h2>Here is your posted data</h2>
<c:forEach var="map_entry" items="${param}">
    <strong><c:out value="${map_entry.key}" /></strong>: 
  <c:out value="${map_entry.value}" /><br><br>
</c:forEach>
</body>
</html>
*/
package com.java2s;

import java.util.Map;
import java.util.Iterator;
import java.util.Map.Entry;

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;
import org.apache.commons.httpclient.HttpException;

public class PostBean implements java.io.Serializable {

private Map parameters;
private String url;

public PostBean(){
}

public void setParameters(Map param){

  if (param != null)
      parameters = param;
} 

public Map getParameters(){

    return parameters;
    }
    
public void setUrl(String url){

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

public String getUrl(){

    return url;
    }

  
public String getPost() throws java.io.IOException,HttpException{

    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;
  
}//end getPost

 private NameValuePair[] getParams(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;
 }//end getParams

}
           
       

Thank with us