The JSP Expression Language

JSTL 1.0 introduced a simple Expression Language (EL) for setting action attribute values based on runtime data from various sources. The specification of this language was later incorporated into the JSP 2.0 specification, so it no longer is just for the JSTL libraries; now it can be used in a portable manner for any tag library, as well as in other contexts than action attributes.

The EL is inspired by JavaScript (or ECMAScript, as it’s formally called) and, to some extent, XPath (a language used to access pieces of an XML document), but it is much more forgiving when a variable doesn’t contain a value (null) and performs more data-type conversions automatically. These features are important for a web application, because the input is mostly in the form of request parameters, which are always text values but often need to be used as numbers or Boolean values (true or false) by the application. A web application must also handle the absence of a parameter gracefully, and the EL makes provisions for this as well. What you don’t find in the EL are statements such as if/else, for, and switch; in JSP, the type of logic implemented by such statements in a general-purpose language are instead implemented as action elements.

To give you a feel for how the EL is used, let’s look at the expression used for the JSTL <c:out> action in Example 5-1:

<c:out value="${1 + 2 + 3}" />

An EL expression always starts with the ${ delimiter (a dollar sign plus a left curly ...

Get JavaServer Pages, 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.