Core Tag Library
Core Tag Library
The set tag sets the value of an EL variable or the property of an EL variable in any of the JSP scopes
(page, request, session, or application). If the variable does not already exist, it is created.
The JSP EL variable or property can be set either from the attribute value:
<c:set var="foo">
...
</c:set>
For example, the following sets an EL variable named bookID with the value of the request parameter
named Remove:
To remove an EL variable, you use the remove tag. When the bookstore JSP page bookreceipt.jsp
is invoked, the shopping session is finished, so the cart session attribute is removed as follows:
To execute flow control logic, a page author must generally resort to using scriptlets. For example, the
following scriptlet is used to iterate through a shopping cart:
<%
Iterator i = cart.getItems().iterator();
while (i.hasNext()) {
ShoppingCartItem item =
(ShoppingCartItem)i.next();
...
%>
<tr>
<td align="right" bgcolor="#ffffff">
${item.quantity}
</td>
...
<%
}
%>
Flow control tags eliminate the need for scriptlets. The next two sections have examples that demonstrate
the conditional and iterator tags.
Conditional Tags
The if tag allows the conditional execution of its body according to the value of the test attribute. The
following example from bookcatalog.jsp tests whether the request parameter Add is empty. If the test
evaluates to true, the page queries the database for the book record identified by the request parameter
and adds the book to the shopping cart:
The choose tag performs conditional block execution by the embedded when subtags. It renders the body
of the first when tag whose test condition evaluates to true. If none of the test conditions of nested when
tags evaluates to true, then the body of an otherwise tag is evaluated, if present.
For example, the following sample code shows how to render text based on a customer's membership
category.
<c:choose>
<c:when test="${customer.category == 'trial'}" >
...
</c:when>
<c:when test="${customer.category == 'member'}" >
...
</c:when>
The choose, when, and otherwise tags can be used to construct an if-then-else statement as
follows:
<c:choose>
<c:when test="${count == 0}" >
No records matched your selection.
</c:when>
<c:otherwise>
${count} records matched your selection.
</c:otherwise>
</c:choose>
Iterator Tags
The forEach tag allows you to iterate over a collection of objects. You specify the collection via the
items attribute, and the current item is available through a variable named by the var attribute.
A large number of collection types are supported by forEach, including all implementations of
java.util.Collection and java.util.Map. If the items attribute is of type
java.util.Map, then the current item will be of type java.util.Map.Entry, which has the
following properties:
key: The key under which the item is stored in the underlying Map
value: The value that corresponds to the key
Arrays of objects as well as arrays of primitive types (for example, int) are also supported. For arrays of
primitive types, the current item for the iteration is automatically wrapped with its standard wrapper class (for
example, Integer for int, Float for float, and so on).
Here's the shopping cart iteration from the preceding section, now with the forEach tag:
The forTokens tag is used to iterate over a collection of tokens separated by a delimiter.
URL Tags
The jsp:include element provides for the inclusion of static and dynamic resources in the same context
as the current page. However, jsp:include cannot access resources that reside outside the Web
application, and it causes unnecessary buffering when the resource included is used by another element.
In the following example, the transform element uses the content of the included resource as the input of
its transformation. The jsp:include element reads the content of the response and writes it to the body
content of the enclosing transform element, which then rereads exactly the same content. It would be more
efficient if the transform element could access the input source directly and thereby avoid the buffering
involved in the body content of the transform tag.
<acme:transform>
<jsp:include page="/exec/employeesList"/>
<acme:transform/>
The import tag is therefore the simple, generic way to access URL-based resources, whose content can
then be included and or processed within the JSP page. For example, in XML Tag Library, import is used
to read in the XML document containing book information and assign the content to the scoped variable
xml:
The param tag, analogous to the jsp:param tag (see jsp:param Element), can be used with import to
specify request parameters.
In Session Tracking we discuss how an application must rewrite URLs to enable session tracking whenever
the client turns off cookies. You can use the url tag to rewrite URLs returned from a JSP page. The tag
includes the session ID in the URL only if cookies are disabled; otherwise, it returns the URL unchanged.
Note that this feature requires that the URL be relative. The url tag takes param subtags to include
parameters in the returned URL. For example, bookcatalog.jsp rewrites the URL used to add a book
to the shopping cart as follows:
The redirect tag sends an HTTP redirect to the client. The redirect tag takes param subtags for
including parameters in the returned URL.
Miscellaneous Tags
The catch tag provides a complement to the JSP error page mechanism. It allows page authors to recover
gracefully from error conditions that they can control. Actions that are of central importance to a page should
not be encapsulated in a catch; in this way their exceptions will propagate instead to an error page.
Actions with secondary importance to the page should be wrapped in a catch so that they never cause the
error page mechanism to be invoked.
The exception thrown is stored in the variable identified by var, which always has page scope. If no
exception occurred, the scoped variable identified by var is removed if it existed. If var is missing, the
exception is simply caught and not saved.
The out tag evaluates an expression and outputs the result of the evaluation to the current JspWriter
object. The syntax and attributes are as follows:
If the result of the evaluation is a java.io.Reader object, then data is first read from the Reader
object and then written into the current JspWriter object. The special processing associated with
Reader objects improves performance when a large amount of data must be read and then written to the
response.
If escapeXml is true, the character conversions listed in Table 14-4 are applied.
< <
> >
& &
' '
" "