How to use the JSP Expression Language (EL) Chapter 10

Chapter 10
How to use the JSP
Expression Language
(EL)
0XUDFK¶V-DYD6HUYOHWV-63QG (G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
Objectives
Applied
• 8VH(/LQ\RXU-63VWRDFFHVVWKHDWWULEXWHVDQGSURSHUWLHVRI
-DYD%HDQVPDSVDUUD\VDQGOLVWV
• 8VH(/ZLWKWKHLPSOLFLW(/REMHFWVWRZRUNZLWKUHTXHVW
SDUDPHWHUVUHTXHVWKHDGHUVFRRNLHVFRQWH[WLQLWLDOL]DWLRQ
SDUDPHWHUVDQGSDJH&RQWH[WREMHFWV
• 'LVDEOHRUHQDEOH(/RUVFULSWLQJ
Knowledge
• 'HVFULEHWKHDGYDQWDJHVDQGGLVDGYDQWDJHVRIXVLQJ(/
• ([SODLQZK\\RXPLJKWZDQWWRGLVDEOH(/RUVFULSWLQJ
0XUDFK¶V-DYD6HUYOHWV-63QG (G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
A JSP that uses EL to access a User object named
user that has been stored in the session object
<table cellspacing="5" cellpadding="5" border="1">
<tr>
<td align="right">First name:</td>
<td>${user.firstName}</td>
</tr>
<tr>
<td align="right">Last name:</td>
<td>${user.lastName}</td>
</tr>
<tr>
<td align="right">Email address:</td>
<td>${user.emailAddress}</td>
</tr>
</table>
0XUDFK¶V-DYD6HUYOHWV-63QG (G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
The same JSP using standard JSP tags
<jsp:useBean id="user"
scope="session"
class="business.User"/>
<table cellspacing="5" cellpadding="5" border="1">
<tr>
<td align="right">First name:</td>
<td><jsp:getProperty name="user"
property="firstName"/></td>
</tr>
<tr>
<td align="right">Last name:</td>
<td><jsp:getProperty name="user"
property="lastName"/></td>
</tr>
<tr>
<td align="right">Email address:</td>
<td><jsp:getProperty name="user"
property="emailAddress"/></td>
</tr>
</table>
0XUDFK¶V-DYD6HUYOHWV-63QG (G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
JSP Expression Language (EL)
• 7KH-63([SUHVVLRQ/DQJXDJH(/SURYLGHVDFRPSDFWV\QWD[
WKDWOHWV\RXJHWGDWDIURP-DYD%HDQVPDSVDUUD\VDQGOLVWVWKDW
KDYHEHHQVWRUHGDVDWWULEXWHVRIDZHEDSSOLFDWLRQ
Advantages of EL
• (/KDVDPRUHHOHJDQWDQGFRPSDFWV\QWD[WKDQVWDQGDUG-63WDJV
• (/OHWV\RXDFFHVVQHVWHGSURSHUWLHV
• (/OHWV\RXDFFHVVFROOHFWLRQVVXFKDVPDSVDUUD\VDQGOLVWV
• (/GRHVDEHWWHUMRERIKDQGOLQJQXOOYDOXHV
• (/SURYLGHVPRUHIXQFWLRQDOLW\
Disadvantages of EL
• (/GRHVQ¶WFUHDWHD-DYD%HDQLILWGRHVQ¶WDOUHDG\H[LVW
• (/GRHVQ¶WSURYLGHDZD\WRVHWSURSHUWLHV
0XUDFK¶V-DYD6HUYOHWV-63QG (G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
An example that accesses an attribute named
currentDate
Syntax
${attribute}
Servlet code
Date currentDate = new Date();
request.setAttribute("currentDate", currentDate);
JSP code
<p>The current date is ${currentDate}</p>
0XUDFK¶V-DYD6HUYOHWV-63QG (G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
An example that accesses the firstName property
of an attribute named user
Syntax
${attribute.property}
Servlet code
User user = new User(firstName, lastName, emailAddress);
session.setAttribute("user", user);
JSP code
<p>Hello ${user.firstName}</p>
0XUDFK¶V-DYD6HUYOHWV-63QG (G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
The sequence of scopes that Java searches to find
the attribute
Scope
SDJH
UHTXHVW
VHVVLRQ
DSSOLFDWLRQ
Description
7KHEHDQLVVWRUHGLQWKHLPSOLFLW3DJH&RQWH[WREMHFW
7KHEHDQLVVWRUHGLQWKH+WWS6HUYOHW5HTXHVWREMHFW
7KHEHDQLVVWRUHGLQWKH+WWS6HVVLRQREMHFW
7KHEHDQLVVWRUHGLQWKH6HUYOHW&RQWH[WREMHFW
0XUDFK¶V-DYD6HUYOHWV-63QG (G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
How to use the dot operator to work with
JavaBeans and maps
• $-DYD%HDQLVDVSHFLDOW\SHRIREMHFWWKDWSURYLGHVDVWDQGDUGZD\
WRDFFHVVLWVSURSHUWLHV
• $PDSLVDVSHFLDOW\SHRIFROOHFWLRQWKDW¶VXVHGWRVWRUHNH\YDOXH
SDLUV)RUH[DPSOHD+DVK0DSFROOHFWLRQLVDPDS
• :KHQ\RXXVHWKHGRWRSHUDWRUWKHFRGHWRWKHOHIWRIWKHRSHUDWRU
PXVWVSHFLI\D-DYD%HDQRUDPDSDQGWKHFRGHWRWKHULJKWRIWKH
RSHUDWRUPXVWVSHFLI\D-DYD%HDQSURSHUW\RUDPDSNH\
• :KHQ\RXXVHWKLVV\QWD[(/ORRNVXSWKHDWWULEXWHVWDUWLQJZLWK
WKHVPDOOHVWVFRSHSDJHVFRSHDQGPRYLQJWRZDUGVWKHODUJHVW
VFRSHDSSOLFDWLRQVFRSH
0XUDFK¶V-DYD6HUYOHWV-63QG (G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
The implicit EL objects for specifying scope
Scope
SDJH
UHTXHVW
VHVVLRQ
DSSOLFDWLRQ
Implicit EL object
SDJH6FRSH
UHTXHVW6FRSH
VHVVLRQ6FRSH
DSSOLFDWLRQ6FRSH
0XUDFK¶V-DYD6HUYOHWV-63QG (G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
An example that specifies request scope
Syntax
${scope.attribute}
Servlet code
Date currentDate = new Date();
request.setAttribute("currentDate", currentDate);
JSP code
<p>The current date is ${requestScope.currentDate}</p>
0XUDFK¶V-DYD6HUYOHWV-63QG (G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
An example that specifies session scope
Syntax
${scope.attribute.property}
Servlet code
User user = new User(firstName, lastName, emailAddress);
session.setAttribute("user", user);
JSP code
<p>Hello ${sessionScope.user.firstName}</p>
How to use EL to specify scope
• ,I\RXKDYHDQDPLQJFRQIOLFW\RXFDQXVHWKHLPSOLFLW(/REMHFWV
WRVSHFLI\VFRSH
• $OORIWKHLPSOLFLW(/REMHFWVIRUVSHFLI\LQJVFRSHDUHPDSV$VD
UHVXOW\RXFDQXVHWKHGRWRSHUDWRUWRVSHFLI\DNH\ZKHQ\RXZDQW
WRUHWXUQWKHREMHFWIRUWKDWNH\
0XUDFK¶V-DYD6HUYOHWV-63QG (G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
The syntax for the [ ] operator
${attribute["propertyKeyOrIndex"]}
An example that works with a JavaBean property
Servlet code
User user =
new User("John", "Smith", "jsmith@gmail.com");
session.setAttribute("user", user);
JSP code
<p>Hello ${user["firstName"]}</p>
0XUDFK¶V-DYD6HUYOHWV-63QG (G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
An example that works with an array
Servlet code
String[] colors = {"Red", "Green", "Blue"};
ServletContext application = this.getServletContext();
application.setAttribute("colors", colors);
JSP code
<p>The first color is ${colors[0]}<br>
The second color is ${colors[1]}
</p>
Another way to write the JSP code
<p>The first color is ${colors["0"]}<br>
The second color is ${colors["1"]}
</p>
0XUDFK¶V-DYD6HUYOHWV-63QG (G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
An example that works with a list
Servlet code
ArrayList<User> users = UserIO.getUsers(path);
session.setAttribute("users", users);
JSP code
<p>The first address on our list is
${users[0].emailAddress}<br>
The second address on our list is
${users[1].emailAddress}
</p>
Another way to write the JSP code
<p>The first address on our list is
${users["0"].emailAddress}<br>
The second address on our list is
${users["1"].emailAddress}
</p>
0XUDFK¶V-DYD6HUYOHWV-63QG (G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
How to use the [ ] operator to access arrays and
lists
• $OLVWLVDVSHFLDOW\SHRIFROOHFWLRQVXFKDVDQ$UUD\/LVW!WKDW
XVHVDQLQGH[WRUHWULHYHDQREMHFWWKDW¶VVWRUHGLQWKHFROOHFWLRQ
• $OWKRXJKWKH>@RSHUDWRUFDQEHXVHGWRZRUNZLWK-DYD%HDQVDQG
PDSVLWLVFRPPRQO\XVHGWRZRUNZLWKDUUD\VDQGOLVWV
• :LWK(/WKHTXRWDWLRQPDUNVDUHUHTXLUHGIRUVSHFLI\LQJD
SURSHUW\LQD-DYD%HDQRUDNH\LQDPDSEXWWKHTXRWDWLRQPDUNV
DUHRSWLRQDOZKHQVSHFLI\LQJDQLQGH[RIDQDUUD\RUDOLVW
0XUDFK¶V-DYD6HUYOHWV-63QG (G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
An example that accesses a nested property
Syntax
${attribute.property1.property2}
Servlet code
Product p = new Product();
p.setCode("pf01");
LineItem lineItem = new LineItem(p, 10);
session.setAttribute("item", lineItem);
JSP code
<p>Product code: ${item.product.code}</p>
0XUDFK¶V-DYD6HUYOHWV-63QG (G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
Another way to access the same property
Syntax
${attribute["property1"].property2}
Servlet code
Product p = new Product();
p.setCode("pf01");
LineItem lineItem = new LineItem(p, 10);
session.setAttribute("item", lineItem);
JSP code
<p>Product code: ${item["product"].code}</p>
How to access nested properties
• ,ID-DYD%HDQKDVDSURSHUW\WKDWUHWXUQVDQRWKHU-DYD%HDQ\RX
FDQXVHWKHGRWRSHUDWRUWRDFFHVVQHVWHGSURSHUWLHV
• 7KHUHLVQROLPLWWRWKHQXPEHURIQHVWHGSURSHUWLHVWKDW\RXFDQ
DFFHVVZLWKWKHGRWRSHUDWRU
0XUDFK¶V-DYD6HUYOHWV-63QG (G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
An example that uses an attribute within the [ ]
operator
Syntax
${attribute[attribute].property}
Servlet code
HashMap<String, User> usersMap =
UserIO.getUsersMap(path);
session.setAttribute("usersMap", usersMap);
String emailAddress =
request.getParameter("emailAddress");
session.setAttribute("emailAddress", emailAddress);
JSP code
<p>First name: ${usersMap[emailAddress].firstName}</p>
JSP code that will return an empty string
<!-- this doesn’t work because the attribute is enclosed
in quotes -->
<p>First name: ${usersMap["emailAddress"].firstName}</p>
0XUDFK¶V-DYD6HUYOHWV-63QG (G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
Another example of the [ ] operator
Syntax
${attribute[attribute[index]].property}
Servlet code
HashMap<String, User> usersMap =
UserIO.getUsersMap(path);
session.setAttribute("usersMap", usersMap);
String[] emailAddresses = {"jsmith@gmail.com",
"joel@murach.com"};
session.setAttribute("emailAddresses", emailAddresses);
JSP code
<p>First name:
${usersMap[emailAddresses[0]].firstName}</p>
JSP code that will return an empty string
<!-- this doesn’t work because the attribute is enclosed
in quotes -->
<p>First name:
${usersMap["emailAddresses[0]"].firstName}</p>
0XUDFK¶V-DYD6HUYOHWV-63QG (G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
How to use the [ ] operator to access attributes
• ,IWKHH[SUHVVLRQZLWKLQWKH>@RSHUDWRULVQ¶WHQFORVHGZLWKLQ
TXRWHV(/HYDOXDWHVWKHH[SUHVVLRQ
• 7RVWDUW(/FKHFNVWRVHHLIWKHH[SUHVVLRQLVDQDWWULEXWH7KHQLW
DWWHPSWVWRHYDOXDWHWKHH[SUHVVLRQ
• ,IPXOWLSOH>@RSHUDWRUVH[LVWWKHH[SUHVVLRQZLOOEHHYDOXDWHGIURP
WKHLQQHUPRVW>@RSHUDWRUWRWKHRXWHUPRVW>@RSHUDWRU
0XUDFK¶V-DYD6HUYOHWV-63QG (G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
Other implicit objects that you can use
EL implicit object Description
SDUDP
$PDSWKDWUHWXUQVDYDOXHIRUWKHVSHFLILHG
UHTXHVWSDUDPHWHUQDPH
SDUDP9DOXHV
$PDSWKDWUHWXUQVDQDUUD\RIYDOXHVIRUWKH
VSHFLILHGUHTXHVWSDUDPHWHUQDPH
KHDGHU
$PDSWKDWUHWXUQVWKHYDOXHIRUWKHVSHFLILHG
+773UHTXHVWKHDGHU
KHDGHU9DOXHV
$PDSWKDWUHWXUQVDQDUUD\RIYDOXHVIRUWKH
VSHFLILHG+773UHTXHVWKHDGHU
FRRNLH
$PDSWKDWUHWXUQVWKH&RRNLHREMHFWIRUWKH
VSHFLILHGFRRNLH
0XUDFK¶V-DYD6HUYOHWV-63QG (G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
Other implicit objects that you can use (cont.)
EL implicit object Description
LQLW3DUDP
$PDSWKDWUHWXUQVWKHYDOXHIRUWKHVSHFLILHG
SDUDPHWHUQDPHLQWKHFRQWH[WSDUDPHOHPHQWRI
WKHZHE[POILOH
SDJH&RQWH[W
$UHIHUHQFHWRWKHLPSOLFLWSDJH&RQWH[WREMHFW
WKDW¶VDYDLODEOHIURPDQ\-63
0XUDFK¶V-DYD6HUYOHWV-63QG (G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
How to get parameter values from the request
An HTML form that has two parameters with the same
name
<form action="addToEmailList" method="post">
<p>First name: <input type="text"
name="firstName"></p>
<p>Email address 1: <input type="text"
name="emailAddress"></p>
<p>Email address 2: <input type="text"
name="emailAddress"></p>
</form>
JSP code
<p>First name: ${param.firstName}<br>
Email address 1: ${paramValues.emailAddress[0]}<br>
Email address 2: ${paramValues.emailAddress[1]}
</p>
0XUDFK¶V-DYD6HUYOHWV-63QG (G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
How to get an HTTP header
JSP code
<p>Browser MIME types: ${header.accept}<br><br>
Browser compression types:
${header["accept-encoding"]}
</p>
The text that’s returned for Internet Explorer 6.0
Browser MIME types: image/gif, image/x-xbitmap,
image/jpeg, image/pjpeg, application/x-shockwave-flash,
application/vnd.ms-excel, application/msword, */*
Browser compression types: gzip, deflate
0XUDFK¶V-DYD6HUYOHWV-63QG (G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
How to work with cookies
Servlet code
Cookie c = new Cookie("emailCookie", emailAddress);
c.setMaxAge(60*60); //set its age to 1 hour
c.setPath("/"); //allow entire application to access it
response.addCookie(c);
JSP code
<p>The email cookie: ${cookie.emailCookie.value}</p>
0XUDFK¶V-DYD6HUYOHWV-63QG (G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
How to get a context initialization parameter
XML in the web.xml file
<context-param>
<param-name>custServEmail</param-name>
<param-value>custserv@murach.com</param-value>
</context-param>
JSP code
<p>The context init param: ${initParam.custServEmail}</p>
0XUDFK¶V-DYD6HUYOHWV-63QG (G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
How to use the pageContext object
JSP code
<p>HTTP request method: ${pageContext.request.method}<br>
HTTP response type:
${pageContext.response.contentType}<br>
HTTP session ID: ${pageContext.session.id}<br>
HTTP contextPath:
${pageContext.servletContext.contextPath}<br>
</p>
An example of the text that might be displayed in the
browser
HTTP
HTTP
HTTP
HTTP
request method: POST
response type: text/html
session ID: 4C1CFDB54B0339B53BE3AC8E9BADC0F5
servletContext path: /ch10email
0XUDFK¶V-DYD6HUYOHWV-63QG (G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
Arithmetic EL operators
Operator
Alternative
GLY
PRG
Description
$GGLWLRQ
6XEWUDFWLRQ
0XOWLSOLFDWLRQ
'LYLVLRQ
0RGXOXVUHPDLQGHU
0XUDFK¶V-DYD6HUYOHWV-63QG (G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
Arithmetic EL operators (cont.)
Example
Result
${1+1}
${17.5+10}
${2.5E3}
${2.5E3+10.4}
${2-1}
${7*3}
${1 / 4}
${1 div 4}
${10 % 8}
${10 mod 8}
${1 + 2 * 4}
${(1 + 2) * 4}
${userID + 1}
2
27.5
2500.0
2510.4
1
21
0.25
0.25
2
2
9
12
9 if userID equals 8; 1 if userID equals 0
0XUDFK¶V-DYD6HUYOHWV-63QG (G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
Relational EL operators
Operator
!
! Alternative
HT
QH
OW
JW
OH
JH
0XUDFK¶V-DYD6HUYOHWV-63QG (G&
Description
(TXDOWR
1RWHTXDOWR
/HVVWKDQ
*UHDWHUWKDQ
/HVVWKDQRUHTXDOWR
*UHDWHUWKDQRUHTXDOWR
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
Relational EL operators (cont.)
Example
Result
${"s1" == "s1"}
${"s1" eq "s1"}
${1 == 1}
${1 != 1}
${1 ne 1}
${3 < 4}
${3 lt 4}
${3 > 4}
${3 gt 4}
${3 <= 4}
${3 >= 4}
${user.firstName == null}
true
true
true
false
false
true
true
false
false
true
false
true if
value
true if
string
true if
isDirty
is null
${user.firstName == ""}
${isDirty == true}
0XUDFK¶V-DYD6HUYOHWV-63QG (G&
firstName returns a null
firstName returns an empty
isDirty is true, false if
is false, false if isDirty
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
Logical EL operators
Operator
__
Alternative
DQG
RU
QRW
Example
${"s1" == "s1"
${"s1" == "s1"
${"s1" == "s1"
${"s1" == "s1"
${"s1" != "s1"
${"s1" != "s1"
${!true}
${not true}
Description
$QG
2U
1RW
Result
&& 4 > 3}
and 4 > 3}
&& 4 < 3}
|| 4 < 3}
|| 4 < 3}
or 4 < 3}
0XUDFK¶V-DYD6HUYOHWV-63QG (G&
true
true
false
true
false
false
false
false
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
Other operators
Syntax
HPSW\[
["\]
Description
5HWXUQVWUXHLIWKHYDOXHRI[LVQXOORUHTXDOWRDQ
HPSW\VWULQJ
,I[HYDOXDWHVWRWUXHUHWXUQV\2WKHUZLVHUHWXUQV]
Example
Result
${empty firstName}
true if firstName returns a null
value or an empty string
s1
s2
${true ? "s1" : "s2"}
${false ? "s1" : "s2"}
0XUDFK¶V-DYD6HUYOHWV-63QG (G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
Keywords you can use in expressions
Keyword
QXOO
WUXH
IDOVH
Description
$QXOOYDOXH
$WUXHYDOXH
$IDOVHYDOXH
0XUDFK¶V-DYD6HUYOHWV-63QG (G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
How to disable EL
For a single page (a page directive)
<%@ page isELIgnored ="true" %>
For the entire application (the web.xml file)
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<el-ignored>true</el-ignored>
</jsp-property-group>
</jsp-config>
Description
• )RU-63DQGODWHUWKHVHUYOHWFRQWDLQHUHYDOXDWHVDQ\WH[WWKDW
VWDUWVZLWK^DQGHQGVZLWK`DVDQ(/H[SUHVVLRQ0RVWRIWKH
WLPHWKLVLVZKDW\RXZDQW,ILWLVQ¶W\RXFDQLJQRUH(/IRUD
VLQJOH-63RUIRUDOO-63VLQWKHHQWLUHDSSOLFDWLRQ
0XUDFK¶V-DYD6HUYOHWV-63QG (G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
How to disable scripting
For the entire application (the web.xml file)
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<scripting-invalid>true</scripting-invalid>
</jsp-property-group>
</jsp-config>
Description
• ,I\RXZDQWWRUHPRYHDOOVFULSWLQJIURPDOO-63VLQ\RXU
DSSOLFDWLRQ\RXFDQPRGLI\WKHZHE[POILOHVRLWGRHVQ¶WDOORZ
VFULSWLQJ7KHQLI\RXUHTXHVWD-63WKDWFRQWDLQVVFULSWLQJWKH
VHUYOHWFRQWDLQHUZLOOGLVSOD\DQHUURUSDJH
0XUDFK¶V-DYD6HUYOHWV-63QG (G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH