CHAPTER 3: Best Practices in Java EE Web Development 109
Line 1: This line creates the instance of the class User using a standard JSP
action.
Line 2: This line introduces the key feature of Expression Language called an EL
expression, which replaces the scripting element called expressions. The syntax
shown in line 2 will be discussed in greater detail in the sections that follow.
JSP standard actions are, in general, far too limited, and consequently developers had to resort to
using scriptlets to create function-rich web applications. Using Java code in the form of scriptlets in
JSP leads to unmaintainable JSP pages. As a result, the JSP specification has evolved to support
Java-free JSP pages. This support rests primarily on the JSP Expression Language (EL) and the JSP
Standard Tag Library (JSTL). In the sections that follow, we will take a closer look at EL and JSTL.
Expression Language
Beauty without expression is lifeless, JSP without Expression Language is chaos. The underlying
principle of the Expression Language is to provide scriptless JSP components. The Expression
Language is used in two ways.
To retrieve objects from scoped attributes (explained in the previous chapter).
These objects are JavaBeans, maps, arrays, and lists that have been stored as
attributes in any of the four scopes (also explained in the previous chapter). The
EL searches the attributes first in the smallest scope, which is page scope; then in
request and session; and finally in the largest scope, which is application scope.
To access request parameters, request headers, cookies, context-initialization
parameters, and pageContext objects.
An EL expression is formed by using the construct${expr} or #{expr}. Even though both the
constructs are evaluated in the same way by the EL, the ${expr} construct is used for immediate
evaluation, and the #{expr} construct is used for deferred evaluation.
Immediate evaluation: The expression is compiled when the JSP page is
compiled, and it is executed when the JSP page is executed.
Deferred evaluation: The expression is not evaluated until its value is needed by
the system.
Note Note In JSP 2.1 and newer, #{} expressions are allowed only for tag attributes that accept deferred
expressions. #{expr} will generate an error if used anywhere else.
In the sections that follow, you will look at the syntax of EL and the reserved words of the language
and see how to use it on a JSP page. After you have learned the basics, you’ll learn how to use EL
to read values from JavaBeans, and in the final section you will learn how to use EL functions.