O'Reilly logo

Java Servlet & JSP Cookbook by Bruce W. Perry

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

7.6. Posting Data from a JSP

Problem

You want to send parameters and their values as an HTTP POST request from a JSP.

Solution

The easiest way to post data from a JSP is to do it the old fashioned way: use the HTML form tag and a Submit button. If you have to send the data dynamically (as in not relying on a user to press a form button), use a JavaBean that encapsulates the HttpClient code discussed in Recipe 7.5.

Discussion

The simplest way to initiate a POST method in a JSP is to set up the HTML template text as shown in Example 7-1: provide an HTML form tag that the user fills out and submits. Since Example 7-1 already shows a typical HTML form, I'l use this space to show a JavaBean that allows a JSP to dynamically post data to another server-side process.

Example 7-10 shows a jspPost.jsp page that uses a PostBean utility class to send a set of parameters/values to another JSP. The receiving JSP, viewPost.jsp, processes the parameters that the PostBean object sends it, then returns some text for the JSP in Example 7-10 to display. The JSP passes the parameters that it wants to post as a java.util.Map to the PostBean class. The PostBean url property is the destination for the posted data (the address that you would otherwise place in the action attribute of a form HTML tag). The code:

<jsp:setProperty name="postBean" property="parameters" value="<%= request.
getParameterMap( )%>" />

gets a Map of the parameters that were passed to the jspPost.jsp page with the HttpServletRequest.getParameterMap( ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required