O'Reilly logo

Java Examples in a Nutshell, 3rd Edition by David Flanagan

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

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, ...

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