24 - JSP Expression Language

24.1 Overview of JSP Expression Language

JSP is primarily used for view purpose. But even to display result of business logic, we need to access the data , process the data, store it in some scope in JSP. We have discussed how to do all these in JSP using scripting tags in earlier chapter.

JSP specification provides another convenient way (Expression Language) to minimize the use of scripting tag in JSP because writing java code is not easy for web designers.

General syntax of expression language is ${expression}

If given attribute is not found or expression returns null instead of throwing any exception. For arithmetic operations, EL treats null as 0 and for logical operations, EL treats null as false.

24.2 Expression Language Implicit Objects

JSP Expression Language provides several implicit objects to get attributes from different scopes and to get parameter values. Below are the implicit objects-

· pageScope- is used to get the value of attribute stored in page scope.

· requestScope- is used to get the value of attribute stored in request scope.

· sessionScope- is used to get the value of attribute stored in session scope.

· applicationScope- is used to get the value of attribute stored in application scope.

· cookie - is used to get the cookie value.

· initParam- is used to get the context init params, we can’t use it for servlet init params

· pageContextit provides access to many objects request, session etc.

· param- is used to get request parameters (valid only for single value)

· paramValues- is used to get request parameter to an array of values

· headeris used to get the request header value (valid for single value)

· headerValues- used to get the request header value to an array of values.

24.3 Expression Language Operators

· Arithmetic Operators - Arithmetic Operators supported by EL are + , - , / or div , * , % or mod

· Logical Operators - EL supports three ( && , || , ! ) logical operators.

· Relational Operators- EL supports == (eq), != (ne), < (lt), > (gt), <= (le) and >= (ge) relational operators.

· Dot (.) Operator- Also known as property access operator. This operator allows us to access property of any bean using dot notation . For example – to access the value of a property with name property_name of bean , we can use below syntax

${bean.property_name}

There can be multiple level of nesting. EL allow to add scope to get the bean from a particular scope like ${requestScope.bean.property} . If we do not add scope, EL searches for the bean in the order of pageà requestà sessionà application scope.

· Collection Access Operators EL supports [] operator which can be used to get the data from array and list along with beans.

Note: If attribute names has dot (.) then we cannot use Dot operator.

For example 

${requestScope["a.b"]}
${myList[1]} or ${myList["1"]}

24.4 Expression Language Reserved Words

There are certain words which are reserved in EL so cannot be used as a JSP identifier. These reserved words are –

· and

· or

· not

· eq

· ne

· lt

· gt

· le

· ge

· true

· false

· null

· instanceof

· empty

· div

· mod

24.6 Disable Expression Language

JSP specification provides a way to disable the evaluation of EL expressions

   <%@ page isELIgnored ="true|false" %>

The valid values of this attribute are true and false. If it is true, EL expressions are ignored .If it is false, EL expressions are evaluated by the container.

24.7 Examples of Expressio Language

24.7.1- Write an example to demonstrate the order of scope that EL follows if not specified.

Solution: Create elScopes.jsp with below content

<html>
  <head>
   <title> EL Scopes </title>
  </head>
  <body>
   <%
    request.setAttribute("person", "Person in Request Scope");
    session.setAttribute("person", "Person in Seccion Scope");
    application.setAttribute("person", "Person in Application Scope");
    pageContext.setAttribute("person", "Person in Page Scope");
   %>
   Value of Person is ${person}
  </body>
</html>

Access elScopes.jsp using http://localhost:8080/jsp-tutorial/elScopes.jsp

24.7.2 Write an example to demonstrate EL implicit objects

Solution –

a) Create a class Fruit like below . (We will use this class for Property access operator)

package com.sample.jsp.tutorial;
public class Fruit {
    private String name;
    private String color;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
}

b) Add context param in web.xml (will be used fot initParam object)

<context-param>
    <param-name>Bonus</param-name>
    <param-value>2000</param-value>
</context-param>

c) Create elImplicitObjects.jsp

<html>
  <head>
   <title> EL Implicit Objects  </title>
  </head>
  <%@ page import="java.util.*" %>
  <%@ page import="com.sample.jsp.tutorial.Fruit" %>
  <body>
   <%   
    Fruit fruit = new Fruit();
    fruit.setColor("Red");
    fruit.setName("Apple");
    request.setAttribute("fruits",fruit);
    List<String> tutorial = new ArrayList<String>();
    tutorial.add("JSP");
    tutorial.add("Servlets");
    pageContext.setAttribute("tutorials", tutorial);
  %>
    <strong>Dot Notation Example:</strong> ${requestScope.fruits.color}
    <br><br>
    <strong>List EL Example:</strong> ${tutorials[1]}
    <br><br>
    <strong>Header information EL Example:</strong> ${header["user-agent"]}
    <br><br>
    <strong>Param EL Example:</strong> ${param["query"]}
    <br><br>
    <strong>pageContext EL Example:</strong> HTTP Method is ${pageContext.request.method}
    <br><br>
    <strong>Context param EL Example:</strong> ${initParam.Bonus}
    <br><br>
  </body>
</html>

d) Access elImplicitObjects.jsp using http://localhost:8080/jsp-tutorial/elImplicitObjects.jsp?query=EL-Example

24.7.3 Write an example to demonstrate EL Operators

Solution : Create elOperators.jsp

<html>
  <head>
   <title> EL Operators  </title>
  </head>
  <body>
   <%
    boolean flag= true ;
   %> 
   <strong>Arithmetic Operator EL Example:</strong> ${initParam.Bonus + 200}
   <br><br>
   <strong>Relational Operator EL Example:</strong> ${initParam.Bonus < 200}
   <br><br>
   <strong>Logical Operator EL Example:</strong> ${flag && false}
   <br><br>
   <strong>Logical Operator EL Example:</strong> ${flag || true}
  </body>
</html>

Access elOperators.jsp using http://localhost:8080/jsp-tutorial/elOperators.jsp

24.7.4 Write an example to disable EL evaluation

SolutionAdd isElIgnored as true in elOpertors.jsp

<html>
  <head>
    <title> EL Operators  </title>
  </head>
  <body>
    <%@page isELIgnored="true" %>
    <%
     boolean flag= true ;
   %> 
   <strong>Arithmetic Operator EL Example:</strong> ${initParam.Bonus + 200}
   <br><br>
   <strong>Relational Operator EL Example:</strong> ${initParam.Bonus < 200}
   <br><br>
   <strong>Logical Operator EL Example:</strong> ${flag && false}
   <br><br>
   <strong>Logical Operator EL Example:</strong> ${flag || true}
  </body>
</html>

Access elOperators.jsp using http://localhost:8080/jsp-tutorial/elOperators.jsp

Like us on Facebook