ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
PC Hardware in  Nutshell, 2nd Edition

JSP 2.0: The New Deal, Part 1

by Hans Bergsten, author of JavaServer Pages, 3rd Edition
11/05/2003

The wait is almost over: the latest version of the JavaServer Pages (JSP) specification, JSP 2.0, is about to be released, along with all of the other J2EE 1.4 specifications. The jump to a new major revision for this JSP version signifies that all of the pieces are now in place for using JSP in a new way: there's no need for Java in the pages, thanks to the new Expression Language (EL) and the JSP Standard Tag Library (JSTL), and reusing code is much easier, thanks to two new ways to develop custom actions. More specifically, JSP 2.0 brings you all of these goodies:

  • The Expression Language first introduced by the JSTL 1.0 specification is now incorporated in the JSP specification, making it available for use with all standard and custom components, as well as in template text.
  • The EL has been extended with a function call mechanism that JSTL 1.1 takes advantage of to make a set of commonly needed functions readily available.
  • JSP error pages now have access to more information about the error, through new variables that are better aligned with the error-handling mechanism defined by the servlet specification.
  • The requirements for how containers report JSP syntax errors have been made stricter to make it easier to find out what's wrong.
  • All J2EE 1.4 specifications, including JSP 2.0 and Servlet 2.4, use XML schema for declaration of the deployment descriptor rules. One benefit of this is that you can now list the declarations in the web.xml file in any order. JSP 2.0 also adds a number of new configuration options to the deployment descriptor, to allow for global configuration instead of per-page configuration.
  • Writing JSP pages as XML documents is now much easier, thanks to more flexible rules and new standard action elements.
  • Custom tag libraries can now be developed as a set of tag files (text files with JSP elements), and tag handlers implemented as Java classes can use a new, simplified tag handler API. At the same time, a number of new features, such as support for a dynamic attribute list and executable fragment attributes, have been added.

Related Reading

JavaServer Pages
By Hans Bergsten

This article is the first in a series of articles describing all of these new JSP 2.0 features. In this part, we'll look at the EL-related news, saving the other features for future installments. I have assumed that you're familiar with JSP 1.2 and have at least heard about the JSP Standard Tag Library (JSTL).

You may also be interested in the third edition of my JavaServer Pages book, where I describe all of this in much more detail and don't assume that you know anything about JSP or JSTL. The book is scheduled for release in December 2003, but you can pre-order it now at Amazon.com, Barnes & Noble, and other online and brick-and-mortar bookstores.

The Expression Language

If you've used JSTL, you're already familiar with the JSP Expression Language. The EL was introduced in the JSTL 1.0 specification to provide an alternative to Java expressions for assigning values to action element attributes at runtime. While the JSTL EL has quickly become very popular, there's a problem: JSTL EL expressions only works with JSTL actions and with custom actions specifically crafted to know how to evaluate EL expressions using non-standard APIs.

Starting with JSP 2.0, the JSP container itself understands EL expressions, making it possible to use them in all places where you previously could only use Java expressions: as the value of attributes for standard and custom actions, and directly in template text.

Before we look at concrete examples, let's take a closer look at what the EL is. The EL is a language inspired by JavaScript, and to some extent, XPath (a language used to access pieces of an XML document), but the EL is much more forgiving about variables without a value (null) and performs more data-type conversions automatically. These features are important for a web application, where the input is mostly in the form of request parameters. The parameters may be present in some requests but not in others, and the browser always sends request parameters as text values, while the application often needs to use them as numbers or Boolean values (true or false). The way the EL is designed, you rarely need to worry about absent values or type conversion at all.

An EL expression contains variables and operators. Any bean stored in one of the JSP scopes (page, request, session, or application) can be used as an EL variable. In addition, the EL supports the following implicit (predefined) variables:

Variable name Description
pageScope A collection (a java.util.Map) of all page scope variables.
requestScope A collection (a java.util.Map) of all request scope variables.
sessionScope A collection (a java.util.Map) of all session scope variables.
applicationScope A collection (a java.util.Map) of all application scope variables.
param A collection (a java.util.Map) of all request parameter values as a single String value per parameter.
paramValues A collection (a java.util.Map) of all request parameter values as a String array per parameter.
header A collection (a java.util.Map) of all request header values as a single String value per header.
headerValues A collection (a java.util.Map) of all request header values as a String array per header.
cookie A collection (a java.util.Map) of all request cookie values as a single javax.servlet.http.Cookie value per cookie.
initParam A collection (a java.util.Map) of all application initialization parameter values as a single String value per parameter.
pageContext An instance of the javax.servlet.jsp.PageContext class, providing access to various request data.

Operators describe what you want to do with the variables. The operators you can use in an EL expression probably look familiar to you if you've used any programming language before, because they are the same as those supported by most languages:

Operator Description
. Access a bean property or Map entry.
[] Access an array or List element.
() Group a subexpression to change the evaluation order.
? : Conditional test: condition ? ifTrue : ifFalse.
+ Addition.
- Subtraction or negation of a value.
* Multiplication.
/ or div Division.
% or mod Modulo (remainder).
== or eq Test for equality.
!= or ne Test for inequality.
< or lt Test for less than.
> or gt Test for greater than.
<= or le Test for less than or equal.
>= or ge Test for greater than or equal.
&& or and Test for logical AND.
|| or or Test for logical OR.
! or not Unary Boolean complement.
empty Test for an empty variable value: null, an empty String, or an array, Map, or Collection without entries).
func(args) A function call, where func is the function name and args is zero, one or more comma-separated function arguments.

An EL expression can also include literals: numbers, text (within single or double quotes), Boolean values and null.

Because an EL expression can appear in many places where static text can also appear, you must tell the JSP container that it should treat an EL expression as such. You do this using delimiters. An EL expression always starts with the ${ delimiter (a dollar sign and a left curly brace) and ends with } (a right curly brace). Here's an EL expression that adds 5 to a variable named amount:

${amount + 5}

If you want to add 5 to the value of a bean property, you use the property access operator:

${order.amount + 5}

The property accessor operator (the dot) tells the EL to look for the named property (amount, in this case) in the specified bean or collection (order, in this case, which can be either a bean or a java.util.Map).

As an alternative, you can use the array element access operator:

${order['amount'] + 5}

The value within the brackets must be either a string literal for the name of the property (as in this example) or a variable (or even a complete EL subexpression) that holds the name of the property.

EL expressions can be used to assign values to any standard or custom JSP action attributes marked as accepting a dynamic value (or request-time attribute value, as it's formally called):

<c:out value="${order.amount + 5}"/>

Prior to JSP 2.0, you had to use a Java expression to assign a dynamic value to an attribute, which has been a common source of confusing syntax errors over the years.

Finally, EL expressions can be mixed with template text directly in the page. This comes in very handy when you're generating HTML elements and need to set an attribute to a dynamic value:

<input name="firstName" value="${customer.firstName}">

With JSP 1.2, you had to use JSTL's <c:out> action to accomplish the same thing, ending up with a mix of elements of different types that was hard to understand:

<input name="firstName" 
    value="<c:out value="${customer.firstName}"/>" >

Pages: 1, 2

Next Pagearrow