JSP Expression Language (EL) :-


Expression Language (EL) provides a simple and shorter ways to access bean properties and attributes (like - request, session, application etc.) in JSP. It was introduces in JSP 2.0.

Syntax - EL expressions are always starts with dollar ($) sign follwoed by curly braces ({}).
${expression}    
Where expression is the EL expression.

For Example - TO access 'name' property of a 'Employee' bean class, we can use the following EL expression -
${emp.name}
where emp is an object of Employee bean class. Note that to access bean property we used .(dot) operator.        


. (dot) operator in EL -

To access bean properties and Map values, EL makes use of .(dot) operator. It has the following syntax -
${exp1.exp2}        
Where expression one can be either an implicit object or an attribute and exp2 can be a bean property (if exp1 is a bean object) or Map value (if exp1 is a Map).

For Example - We have the following HashMap in a Servlet file -
java.util.HashMap fruit_color = new java.util.HashMap();
   fruit_color.put("apple", "red");
   fruit_color.put("banana", "yellow");
   fruit_color.put("gavava", "green");
request.setAttribute("MyFruits", fruit_color);
In JSP file we can access Map values using EL as follows -
 Apple color is - ${MyFruits.apple}
 Banana color is - ${MyFruits.banana}
 Grapes color is - ${MyFruits.grapes}    


[] operator in EL -

In addition to access bean and Map values, [] operator provides option to access array and List etc also. It has the following syntax -
${exp1["exp2"]}        


For Example -
i- We have the following array in a Servlet file -
String fruits[] = {"apple", "orange", "banana", "grapes"};
request.setAttribute("MyFruits", fruits);    
In JSP file we can access array values using EL as follows -
I have  - ${MyFruits["0"]} and ${MyFruits["1"]}   
ii - We have the following ArrayList in a Servlet file -
java.util.ArrayList fruits = new java.util.ArrayList();
    fruits.add("apple");
    fruits.add("orange");
    fruits.add("banana");
    fruits.add("grapes");
request.setAttribute("MyFruits", fruits);  
In JSP file we can access array values using EL as follows -
I have  - ${MyFruits["0"]} and ${MyFruits["1"]}   


EL implicit Objects -

Expression Language provides sevral implicit object that makes works easy -
Implicit Object Type Description
pageScope Map Used to access attributes that has page scope.
requestScope Map Used to access attributes that has request scope.
sessionScope Map Used to access attributes that has session scope.
applicationScope Map Used to access attributes that has aplication scope.
param Map Used to access request parameters. It is similar to 'request.getParameter()' used in servlets.
paramValues Map Used to access multivalued request parameters. It is similar to 'request.getParamataerValues()' used in servlet.
initParam Map Used to access context initialization parameters.
cookie Map Used to access cookies value.
header Map Used to access HTTP request header. It is similar to 'request.getHeader()'
headerValues Map Used to access all HTTP request headers as array. It is similar to 'request.getHeaders()'
pageContext PageContext It is exactly same as JSP PageContext.

EL implicit Objects Examples -


1- pageScope -

index.jsp -
    <body>
        <%
        pageContext.setAttribute("Name", "Rohit Kamboj");
        %>
        
        <h1>Hello Mr. ${pageScope.Name}</h1> 
    </body>
Output -
pageScope EL implicit object

Download this example (NetBeans implementation) - pageScope Example

2- sessionScope -

index.jsp -
<body>
   <%
    session.setAttribute("Name", "Rohit Kamboj");
   %>
   <a href="Result.jsp">Click Here</a>
</body>    
Result.jsp -
<body>
    <h1>Hello Mr. ${sessionScope.Name}</h1>
</body>    
Output -
sessionScope EL implicit object

Download this example (NetBeans implementation) - sessionScope Example

3- param and paramValues -

index.jsp -
<body>
    <form method="post" action="Result.jsp">
      Name - <input type="text" name="t1"><br>
      Roll - <input type="text" name="t2"><br>
      Subject1 - <input type="text" name="subject"><br>
      Subject2 - <input type="text" name="subject"><br>
      Subject3 - <input type="text" name="subject"><br>
      <input type="submit">
    </form>
</body>    
Result.jsp -
<body>
    Name - ${param.t1}<br>
    Roll Number - ${param.t2}<br>
    Subjects - ${paramValues.subject[0]},${paramValues.subject[1]}, ${paramValues.subject[2]} 
</body>    
Output -
param and paramValues EL implicit objects param and paramValues EL implicit objects

Download this example (NetBeans implementation) - param and paramValues Example

4- initParam -

web.xml -
<context-param>
    <param-name>Name</param-name>
    <param-value>Tom</param-value>
</context-param>
<context-param>
    <param-name>Email</param-name>
    <param-value>Tom@gettechnotes.com</param-value>
</context-param>    
index.jsp -
<body>
    Name - ${initParam.Name}<br>
    Email - ${initParam.Email}
</body>    
Output -
initParam EL implict object

Download this example (NetBeans implementation) - initParam Example

5- cookie -

index.jsp -
<body>
  <%
    Cookie c1 = new Cookie("name", "Tom");
    Cookie c2 = new Cookie("email", "tom@gettechnotes.com");
    response.addCookie(c1);
    response.addCookie(c2);
  %>
 <a href="Result.jsp">Click Here</a>
</body>    
Result.jsp -
<body>
   Name - ${cookie.name.value}<br>
   Email - ${cookie.email.value}
</body>    
Output -
cookie expression language implicit object

Download this example (NetBeans implementation) - cookie Example

6- header -

index.jsp -
<body>
    Host - ${header.host}<br>  <!-- or ${header["host"]}   -->
    User Agent - ${header["user-agent"]}   
</body>
Output -
header expression language implicit object

Download this example (NetBeans implementation) - header Example

7- pageContext -

index.jsp -
<body>
    Request Method is - ${pageContext.request.method}  
</body>    
Output -
pageContext expression language implicit object

Download this example (NetBeans implementation) - pageContext Example


EL Operators -

Type Operators
Arithmatic operators '+' , '-' , '*' , '/' or 'div' , '%' or 'mod'
Logical operators '&&' or 'and' , '||' or 'or','!' or 'not'
Relational operators '==' or 'eq' , '!=' or 'ne' , '<' or 'lt' , '>' or 'gt' , '<=' or 'ge' , '>=' or 'le'
empty empty operators checks whether a value is null or empty. Returns true if value is null or empty and false otherwise

Examples -

EL Expression Result
${2+3} 5
${5/0} or ${5 div 0} infnity (Note - Dividing by zero is not an exception in EL)
${4 % 3} or ${4 mod 3} 1
${4 % 0} or ${4 mod 0} divide by zero exception
${8 + Tom} 8 (Note - In arithmatic expressions, EL trets unknown values as zero.)
${8/Tom} infnity
${5 > 3} true
${5 > Tom} false (Note - In logical expressions, EL trets unknown values as false)
${5 < Tom} false
${empty param.name} true if name parameter does not have any valye, false otherwise
Download this example (NetBeans implementation) - EL operators Example


Precedence of EL Operators

The precedense of EL operators from higher to lower, left to right is given below -
EL operator Precedence from higher to lower, left to right
[] .
()
- (unary) not ! empty
* / div % mod
+ -
< > <= >= lt gt le ge
== != eq ne
&& and
|| or
? :