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

16.12. Accessing or Removing Request Attributes in JSPs

Problem

You want to use a JSP to access or remove a request attribute.

Solution

Use the JSTL core tags c:out and c:remove to access and optionally remove the attribute.

Discussion

Example 16-14 accesses an object attribute that is bound to the HttpServletRequest. The JSP accesses this attribute by using EL syntax inside the c:out JSTL tag.

Note

Example 16-12 in Recipe 16.10 forwards a request attribute to a servlet using the jsp:forward standard action. The JSP in that example can forward its request attribute to the JSP in Example 16-14 by using the code:

<jsp:forward page="/requestDisplay.jsp" />

The code:

"${requestScope[\"com.jspservletcookbook.ContextObject\"].
    values}"

uses the requestScope JSTL implicit object. This variable, which the JSTL automatically makes available to EL-related code, is a java.util.Map type containing any attributes bound to the request scope. The code then displays the values the attribute contains by accessing the object attribute's values property (see Recipe 16.1 for a discussion of the object used for storing an attribute in various scopes throughout this chapter).

Example 16-14. Accessing and removing a request attribute with the JSTL

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>

<html>
<head><title>Request reading JSP</title></head>
<body>
<h2>Here are the values from the bound RequestObject</h2>

<c:out value= "${requestScope[\"com.jspservletcookbook.ContextObject\"]. values}" escapeXml="false" ...

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