JSP post : Basics « JSP « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » JSP » BasicsScreenshots 
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.Entryiter.next();
             pairs[inew NameValuePair(
                       (String)me.getKey(),((String[]) me.getValue())[0]);
            i++;
          }
          return pairs;
 }//end getParams

}
           
       
Related examples in the same category
1. JSP Passing Parameters
2. Simplest Jsp page: A Web Page
3. Output: Creating a Greeting
4. Simple JSP outputSimple JSP output
5. Simple JSP page to display the random number
6. Comments in JSP page
7. Declaration Tag Example
8. Declaration Tag - Methods
9. Expression Language Examples
10. Passing parameters
11. JSP Initialization
12. Welcome page and top level URL
13. JSP Expression Language
14. JSP without beans
15. Request header display in a JSP
16. Multiple Declaration
17. Embedding Code
18. pwd -- print working directory
19. JSP Post Data Viewer
20. JSP in J2EE
21. JSP Performance
22. JSP Best Practices and Tools
23. JSP Model 2JSP Model 2
24. JSP: expression language 2JSP: expression language 2
25. JSP Basics: Dynamic Page Creation for Data Presentation 2JSP Basics: Dynamic Page Creation for Data Presentation 2
26. JSP Directives: your page
27. JSP Directives
28. JSP Basics ch02 JSP Basics ch02
29. JSP Basics: Generalized Templating and Server Scripting 1JSP Basics: Generalized Templating and Server Scripting 1
30. JSP Basics: Generalized Templating and Server Scripting 2JSP Basics: Generalized Templating and Server Scripting 2
31. JSP Basics: Generalized Templating and Server Scripting 3JSP Basics: Generalized Templating and Server Scripting 3
32. CSS, JavaScript, VBScript, and JSP 1CSS, JavaScript, VBScript, and JSP 1
33. CSS, JavaScript, VBScript, and JSP 2CSS, JavaScript, VBScript, and JSP 2
34. Advanced Dynamic Web Content Generation. 1Advanced Dynamic Web Content Generation. 1
ww__w_.___j___a_v_a_2_s__.___c___om_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.