118 CHAPTER 3: Best Practices in Java EE Web Development
Line 1: In Line 1, the class User is created using the <jsp:useBean> action. The
user was set as the request attribute in the servlet code.
Line 2: In Line 2, the value of name, which is a property of the User object is
accessed using ${user.name}.
The variable user in the expression ${user.name} is an attribute stored in the request scope. The
variable in the EL expression to which the dot operator is applied could be one of two kinds.
An attribute that is stored in any of the four scopes, as it is in this case
An EL implicit object, as explained later in this chapter
Whether this variable is an EL implicit object or an attribute stored in any of the four scopes, it
can be either a JavaBean or a map. In Listing 3-13, this variable is a JavaBean that was set as an
attribute in the request scope, and therefore name is the property of the JavaBean referenced by
the variable user. Had the user variable been a map set as an attribute in any of the four scopes, the
name would-be the key of the map.
Note The variable in the EL expression to which the dot operator is applied could be either an attribute
set in any of the four scopes or an EL implicit object. Furthermore, regardless of whether this variable is an
attribute set in any of the four scopes or is an EL implicit object, it could be either a JavaBean or a Map. If the
variable is a JavaBean, it is followed by its property right after the dot operator; if the variable is a Map, it is
followed by its key right after the dot operator.
So, now it should be clear that the variable in the EL expression is either a JavaBean or a Map. But
what if you want to set an array as an attribute in any of the four scopes and access its elements
using an EL expression? Or what if you want to set a list as an attribute in any of the four scopes and
access its elements using an EL expression? The answer lies with the [] operator provided by EL.
The []operator is used for accessing arrays, lists, JavaBeans, and maps. That is, the variable to
which the [ ] operator is applied can be an array, list, JavaBean, or map.
Inside the Brackets
There can be one of following inside the brackets of the [ ] operator:
An index with or without quotes.
A String literal.
An EL implicit object or an attribute in any of the four scopes. The EL implicit
object is explained later in this chapter.
A nested expression.
If there is an index with or without quotes inside the brackets of the[ ] operator, the variable to which
the [ ] operator is applied is either an array or a list. Listing 3-14 illustrates how to use the [ ] operator
with a list or an array.