Expression Language: Advance Java
Expression Language: Advance Java
Expression Language
LEVEL – PRACTITIONER
About the Author
2
Icons Used
Hands on
Questions Tools Exercise
Best Practices
Demonstration & Industry
Workshop
Standards
3
Objectives
4
Expression Language (EL)
It can also be used to access the values from implicit objects like page
context , header , cookie etc.
Expression languages are always specified within curly braces and prefixed
with a dollar sign.
Example : ${person.name}
5
Advantages of EL
Concise access to stored objects : Easy to access the attributes stored in session,
request or application scope.
Example :${name} search the PageContext, HttpServletRequest, HttpSession, and
ServletContext (in that order) for an attribute named name.
Equivalent to <%= pageContext.findAttribute("name") %>
Short hand notation for bean properties : Easy to access bean properties.
Example : To print the property phoneNumber of a userBean we can use $
{user.phoneNumber} instead of <%=user.getPhoneNumber()%>
Simple access to collection elements To access an element of an array, List, or Map, you
use ${variable[indexOrKey]}.
Example: Consider an arraylist myList
${myList[0]} , Gets the first element in the list and prints it.
6
Advantages of EL (Cont)
7
EL Implicit Objects in a nutshell
Object Description
pageContext The pageContext object refers to the PageContext of the current page.
param To access a single value request parameter.
paramValues Contains the array of parameter values
header Contains the header values as a map
headerValues Contains complete header values
cookie Returns the incoming cookies as a map
initParam For easy access of initialization parameters as a map
pageScope Contains the page scope values as a map
requestScope Contains the request scope values as a map
sessionScope Contains the session scope values as a map
applicationScope Contains the application scope values as a map
The [ ] operator can be used instead of the period operator, lets look at few
examples,
2.To access Map Values: ${country[“ind”]} retrieves the value for the
key ind in the country map. Equivalent to ${country. Ind}
3.To access elements from arrays and list: ${fruits[1]} retrieves the
value at index 1 in the fruits array (list).
10
EL – Arithmetic Operators
11
EL – Logical Operators
12
EL – Relational Operators
13
How to Read Request Parameter Values ?
${param.userName} or ${param[“userName”]}
Reads the value of the parameter named userName set in the request
${paramValues.hobbies[0]} or ${paramValues.hobbies[0]}
14
How to read attributes ?
${sessionScope.name}
15
How to Access Bean Properties ?
${customer.firstName}
16
How to access Collection Objects?
${attributeName[entryName]}
17
Accessing Implicit Objects using EL in a Nutshell.
18
Deactivating Expression Evaluation
Setting this parameter to “True” , EL tags will not be interpreted by the container.
Will print the EL tags as it is in the JSP page.
19
When to Use EL?
EL should be used for accessing and presenting data from implicit objects.
20
A Simple Lend A Hand On EL.
4. Usage of [ ] Operator.
21
Lend a Hand - Scenario
Components to be developed:
1. Registration.jsp : Renders the registration form page
2. RegistrationServlet : Handles the registration process and forwards the
request to success.jsp.
3. User Bean - To hold the user registration details.
4. success.jsp : Prints the user Registration details.
NOTE: For the demo purpose some values irrelevant to the problem statement is
also printed such as the user-agent . This is purely for the associates to
understand.
22
Lend a Hand : Registration page design
23
Lend a Hand : registration.jsp code
24
Lend a Hand : User – Bean Class
25
Lend a Hand : Develop RegistrationServlet
Develop the registration servlet and set a count variable in request, session and
application context.
26
Lend a Hand : Develop Success.jsp
27
Lend a Hand : Success.jsp
29
Advance Java