Hello JSP2
The JSP 2.0 specification is a major step forward from the 1.x
series. It includes support for a simple Java-like expression language
(known as “EL”), which replaces awkward <%=...%>
tags, and supports the
Java Standard Tag Libraries (JSTL), which obviate the
need for most <%...%>
scriptlets. Example 20-5 is
a version of Example 20-4,
rewritten using these features. Note that <c:choose>
and <c:when>
tags serve as an if/else
statement and replace the awkwardly
intermingled HTML tags and Java scriptlets of Example 20-4. Also, notice that EL
expressions are enclosed in ${...}
,
which is quite a bit friendlier than <%...%>
. In this example, the @page
directive is joined by an @taglib
directive specifying that the page
uses the standard “core” tag library and declares its XML namespace to
be “c”.
Example 20-5. hello2.jsp
<%@page contentType='text/html'%> <%-- The taglib directive specifies that we're using the JSTL 1.1 core taglib. -- If you're using 1.0, change to "http://java.sun.com/jstl/core_rt" --%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <c:choose> <c:when test='${param.name == null}'> <form> <i>Please enter your name: </i> <input name="name"/> <input type="Submit"/> </form> </c:when> <c:otherwise> Hello ${param.name}! </c:otherwise> </c:choose>
Expression Language Syntax
A full tutorial on the Expression Language used in JSP 2 pages is beyond the scope of this chapter; you can find complete details in the JSP 2.0 specification. For now, however, ...
Get Java Examples in a Nutshell, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.