Saturday, 15 September 2012

Expression Language

Everything started to resolve the issue:   How to generate dynamic contents ?

And this is how we progressed:

  • Servlets: we handled logic as well as generated HTML from plain java class     i.e. servlets.
  • JSP: Generating HTML from java class was hard and time consuming. So concept of JSP was introduced i.e. to embed java code in HTML file using scriptlets.
  • JavaBeans & custom tags: To maintain large applications, moved most of  the java code from JSPs to JavaBeans. But still we used scriptlets to complete the task or created custom tags to replace those scriptlets.

To further simplify the presentation layer, Expression Language was introduced. It is not a programming or scripting language, provides a way to simplify expressions in JSP. It is a simple language that is towards looking up objects, their properties and performing simple operations on them.

Lets consider this below example of a JSP (without EL). We have to declare a variable before using it, data type must be known in advance and have to use awkward syntax:
       Employee emp = (Employee)request.getAttribute("employee");
     Employee firstName: <%= emp.getFirstName() %>
     <% if(emp.getLastName.equals("...")) %>
     <% } %>

Contrary to the above code, below code gives a hint how useful EL can be. 
In EL, we have easier syntax, all application data are easily accessible and even there is no need to type-cast or use getAttribute method.

         Employee First Name: ${emp.firstName} 
         <c:if test = "${emp.lastName == param.lName}">

EL Syntax
The format of writing any EL expression is:   $ { validExpression }

The valid expressions can consist on these individuals or combination of these given below:
   Literals: Boolean, Integer, Floating Point, String & Null.
   Operators: Arithmetic, Logical, Relational
   Variables (object references), or
   Implicit call to function using property name.

EL Identifiers & Implicit Objects
Identifiers represent the names of objects stored in one of the JSP scopes: page, request, session, or application.

EL has 11 reserved identifiers, corresponding to 11 implicit objects. All other identifiers assumed to refer to scoped variables. These implicit objects are:
  1. pageContext - The context for the JSP page, used to access the JSP implicit objects such as request, response, session, out, servletContext etc.
  2. pageScope - A Map associating names & values of page scoped attributes
  3. requestScope - Map associating names & values of request scoped attributes
  4. sessionScope - Map associating names & values of session scoped attributes
  5. applicationScope - A Map associating names & values of application scoped attributes
  6. param - Maps a request parameter name to a single String Request parameter value.
  7. paramValues - Maps a request parameter name to an array of values
  8. header - Maps a request header name to a single header value.
  9. headerValues - Maps a request header name to an array of value.
  10. cookie - A Map storing the cookies accompanying the request by name
  11. initParam - A Map storing the context initialization parameters of the web application by name
Storing and Retrieving Scoped Variables
Using pure servlets or JSP, we can store variables in a particular scope. For example consider this below code for storing an object of EmployeeInfo in session scope:

         HttpSession sess = request.getSession(true);
         EmployeeInfo e = new EmployeeInfo();
         sess.setAttribute(“employee” , e);

When EL encounters an identifier, it searches for a scoped variable with that name first in page scope, then in request scope, then in session scope and finally in application scope. (If no such object is located in four scopes, null is returned).

For example, if we’ve stored  EmployeeInfo object 'e' in session scope and have written the following EL expression to access the name property of 'e'


Then EL searches for 'e' first in page scope, then in request scope, then in session scope where it found 'e'. After that it calls e.getFirstName() method.

EL Accessors
We can use dot (.) and bracket ([ ]) operators in EL to access identifies and their properties. Dot operator is used for accessing the properties of an object and the bracket operator is used for arrays and collections.
In   ${e.firstName}, EL accesses the object’s properties using JavaBeans conventions therefore getFirstName() must be defined in EmployeeInfo.

Bracket ([]) operator - We can use this operator with arrays like:
     ${ employeeList [2] }   returns 3rd element stored in it.

This can also be used Hash Maps. eg:
     ${ myMap[“id”] }   returns the value associated with the id(key).

Some Other Features of EL
EL supports automatic type conversion. So there is no need to wrap or unwrap primitives from their corresponding java classes. For example:
eid = ${ emp.empId }
Here, if eid is of type 'int' and empId is 'Integer' then there is no need to unwrap this. This will be done automatically behind the scenes.

Also, if object or identifier is null, no NullPointerException would be thrown and the result would be null.

No comments:

Post a Comment