Nearby lessons
27 of 30JSP Examples - Expression Language (EL)
- Read request data with EL instead of scriptlets
- Master EL operators and precedence
- Declare and invoke custom EL functions
EL demo programs: implicit objects (param, header, cookie, initParam, scopes), operators, precedence, the empty operator, EL versus null, and custom EL functions with TLD declarations.
EL Implicit Objects
(But not Servlet Initialization Parameters)
- pageScope,requestScope,sessionScope and applicationScope
We can use these implicit objects to retrieve attributes of a particular scope
| pageScope | pageContext.getAttribute() |
|---|
requestScope request.getAttribute()
sessionScope session.getAttribute()
applicationScope application.getAttribute()
Eg1:
To get the value of session scoped attribute x
${sessionScope.x}
Eg2:
To get the value of request scoped attribute x
${requestScope.x}
Eg3:
${x}
JSP Engine first checks in page scope for the attribute "x". If it is available then JSP Engine prints
its value. If it is not available then JSP engine will search in request scope followed by session
scope and application scope. It is exactly same as findAttribute()
test.jsp:
EL Implicit Objects
Output:
Pavan
ravi
sunny
anushka
Pavan
- param and paramValues
We can use these implicit objects to retrieve request form parameters.
param getParameter()
paramValues getParameterValues()
${param.x}
It prints the value of form parameter x
If the parameter associated with multiple values then we will get only first value.
If the specified parameter not available then we will get blank space
${paramValues.x}
Internally it returns String[] which contains all values associated x. as we are printing that String[],
internally toString() method will be called, which prints result in the following form
[Ljava.lang.String;@1270b73
${paramValues.x[0]} It prints first value of x
${paramValues.x[1]} It prints second value of x
${paramValues.x[100]} prints blank space as EL suppresses ArrayIndexOutOfBoundsException
Demo Program
- header and headerValues:
These are exactly same as param and paramValues except that these are for retrieving request
headers.
header request.getHeader()
headerValues request.getHeaders()
Eg:
<h1>${header.accept}<br>
<h1>${header.host}<br>
<h1>${headerValues.host[0]}
- cookie implicit object :
By using this implicit object we can get cookies associated with the request.
cookie request.getCookies()
<h1>${cookie.JSESSIONID.name}<br>
${cookie.JSESSIONID.value}
- initParam implicit object:
We can use initParam implicit object to access context parameters declared in web.xml
initParam context.getInitParameter()
Demo Program
Note:
If the specified initParameter is not available then we will get blank space b'z EL suppresses null
- pageContext implicit object:
This is the only common implicit object between JSP and EL.
This is the only EL implicit object which is a non-map object.
By using this implicit object we can access all other JSP implicit objects in EL.
| Eg: | invalid |
|---|---|
| ${request.method} | valid |
| ${pageContext.request.method} | invalid |
| ${session.id} | valid |
${pageContext.session.id}
In EL, request and session implicit objects are not available.
Note: El handles null and ArrayIndexOutOfBoundsException very nicely and prints blank space
EL Operators
EL contains its own specific operators. The following is the list of all possible operators.
Demo Program
Syntax: ${leftvariable.rightvariable}
Left Variable Map OR Bean
Right Variable Map Key OR Property
Should be Valid Java Identifier
Eg:
| ${customer.name} | valid |
|---|
${pageContext.request.method} valid
| ${initParam.user} | valid |
|---|---|
| ${customer.1234} | invalid |
Demo Program
Syntax: ${leftvariable[rightvariable]}
| map key | Should be enclosed either in Single OR Double Quotes |
|---|
bean property
array index
list index Quotes are Optional
key or property should be enclosed in either single quotes or double quotes. For index quotes are
optional.
Case-1:working with Map:
${initParam["user"]}
${initParam['user']}
${initParam[user]} blank space
If we are not keeping quotes EL assumes that the key is an attribute stored in some scope. If that
attribute is not available then we will get blank space as output.
Q. Which of the following is the valid way of retrieving request header "accept" value?
Demo Program
Case-2: working with Bean:
${customer['name']}
${customer["name"]}
${customer[name]} blank space as o/p
case-3: Working with arrays:
Demo Program
Case-4: working with List objects:
Demo Program
Note: where ever property access operator is required there we can use collection access
operator. But where ever collection access operator is required we may not use property access
operator
3.Arithmetic operators:
1.+ operator:
EL does not support overloading and hence there is no string concatenation operator. "+" always
acts as addition operator
Eg:
${2+3} 5
| ${"abc"+3} | RE: NumberFormatException |
|---|
${abc+3} 3
${""+3} 3
${null+3} 3
Note:
- In Arithmetic operators null is treated as "0"
- Empty String is also treated as "0"
- - operator:
All rules are exactly same as + operator
${10-3} 7
${"10"-3} 7
${"abc"-3} RE:NFE
${abc-3} -3
- * operator:
All rules are exactly same as + operator
${10 *3} 30
Demo Program
All rules are exactly same as + operator except that division operator always follows floating point
arithmetic
${10 / 2} 5.0
${10 div 2} 5.0
${10 / 0} Infinity
${0 / 0} NaN
Demo Program
In the case of mod operator both floating point and integral arithmetic are possible.
${10%0} ArithmeticException
${10%3} 1
Relational operators:
> or gt
< or lt
== or eq
>= or ge
<= or le
!= or ne
${10>3} true
${abcd==abc} true
EL operator precedence
3.Relational
4.logical operators
5.conditional operator
EL versus null
- Writing tag library descriptor file(tld file):
For EL functions, tld file provides mapping between java class (where functionality is available) &
JSP (where functionality is required).
We can configure EL function by using <function> tag in the tld file.
<function> tag contains the following 4 child tags
EL versus null
By using this name only we can call EL functionality in the JSP.
This name need not be original name of the method
EL versus null
The fully qualified name of java class where EL function is defined
EL versus null
The signature of EL function which is defined in java class.
Here we have to provide return type, method-name & argument list
Eg: myfunctions.tld:
EL versus null
- Write taglib directive:
we can write taglib directive to make EL Functions available to the JSP
<%@ taglib prefix="mime" uri="DiceFunctions" %>
- Write EL function call:
${ mime:rollIt()}
Demo Program
DiceRoller.java
elApp1
|-test.jsp
|-WEB-INF
|-MyFunctions.tld
|-classes
|-pack1
|-DiceRoller.class
Execution Flow of EL Function
EL Function call in JSP
Taglib Directive with
Matched Prefix
TLD File with
Matched URI
Java Class with Matched
function-class & Signature
- Whenever JSP engine encounters EL Function call with Prefix and EL Function Name, then it
checks for corresponding taglib directive with matched prefix.
- From taglib directive JSP engine identifies uri and checks for tld file with matched uri.
- From the TLD file JSP engine identified java class and method signature.JSP engine will check for
the matched java class and method signature.
- JSP Engine executes that method and return its results to JSP.
Demo Program-2:
Write application to invoke Math class sqrt() method as EL Function call with method name m1().
public static double sqrt(double d)
test.jsp:
DiceRoller.java
MyTld.tld
|-test.jsp
|-WEB-INF
|-MyTld.tld
- EL suppresses null output
- EL functions must be declared in a TLD
- empty covers both null and zero-length values