Java Reference
In-Depth Information
LISTING 21.12
Continued
17:
18: </body>
19: </html>
The
hello.jsp
page looks for a parameter called
name
specified in the address (URL) of
the page. For example, the URL http://example.com/hello.jsp?name=Sailor gives
name
the value
Sailor
and causes the page to display the response “Hello, Sailor.” When the
name parameter is omitted, “Hello, Stranger” displays instead.
The first JSP code in the page is the following directive:
<%@ taglib uri='http://java.sun.com/jsp/jstl/core ' prefix='c' %>
Like other tag libraries, each JSTL library must be made available using a directive on
the page before any of its tags can be employed. The preceding directive makes the core
library available on the page. All tags from this library will be prefaced with the text “c:”
followed by the tag's name, as in this example:
<c:out value='${param.name}'/>
JSTL tags are called
actions
, in recognition of the fact that they generate dynamic web
content. Like XML elements, actions can stand alone (
<
tagname
/>
) or be paired (
<tag-
name>
…
</
tagname
>
).
The
c:out
action displays the contents of the local or instance variable indicated by its
value
attribute.
The variable is specified using JSTL's Expression Language, a simple data-access syntax
that borrows from ECMAScript (JavaScript) and XPath. Statements that use the language
are contained within “${“ and “}” characters.
In the preceding example, the variable
param
is one of several standard Expression
Language variables that contain information about the page, web application, and servlet
container. The
param
variable is a collection that holds all of the page parameters, each
represented as a string.
The rest of the page contains three core actions used to create a conditional block:
<c:choose>
<c:when test='${!empty param.name}'>
<h2>Hello, <c:out value='${param.name}'/></h2>
</c:when>
<c:otherwise>
<h2>Hello, stranger</h2>
</c:otherwise>
</c:choose>
21