Recipe 7.8 Using a JSP to Add a Parameter to a Query String


Problem

You want to use a JSP to add one or more parameters to a query string, then forward the request to its destination.

Solution

Use the jsp:forward and jsp:param standard actions.

Discussion

Adding one or more parameters and forwarding to another component is as easy as four lines in a JSP. The jsp:forward action adds any jsp:params to existing parameters when it forwards this text to the processing component, as shown in Example 7-13.

Example 7-13. Adding parameters and forwarding in a JSP
 <jsp:forward  page="/viewPost.jsp" >     <jsp:param name="inspector-name" value="Jen"/> <jsp:param name="inspector-email" value="jenniferq@yahoo.com"/> </jsp:forward> 

If this JSP is requested with the following URL:

 http://localhost:8080/home/addParam.jsp?first=Bruce&last=Perry&zip=01922 

then the three original parameters ( first , last , and zip ) are preserved when the jsp:forward action adds two additional parameters ( inspector-name , inspector-email ) and forwards the page. In the example, the page is processed by the viewPost.jsp page shown in Example 7-3. Requesting addParam.jsp in a browser forwards the request, and a total of five parameters to the viewPost.jsp page. Figure 7-5 shows the result in a browser.

See Also

Recipe 7.2 on handling a POST request in a JSP; Recipe 7.3 on setting the properties of a JavaBean to form input; Recipe 7.6 on posting data from a JSP.



Java Servlet & JSP Cookbook
Java Servlet & JSP Cookbook
ISBN: 0596005725
EAN: 2147483647
Year: 2004
Pages: 326

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net