JSP – Expression Language (EL)

JSP Expression Language (EL) makes it possible to easily access application data stored in JavaBeans components. JSP EL allows you to create expressions of arithmetic and logical. Within a JSP EL expression, you can use integers, floating point numbers, strings, the built-in constants true and false for boolean values, and null.

The Syntax of JSP Expression Language
${OneObject.anotherobject.PropertyName}

Following are various EL implicit objects.

  1. param
  2. paramValues
  3. header
  4. headerValues
  5. cookie
  6. pageScope
  7. requestScope
  8. sessionScope
  9. applicationScope
  10. initParam
  11. pageContext

param and paramValues

  • These two EL implicit objects are used to collect the request parameters.
  • When use param then internally request.getParameter() method will be called.
  • When use paramValues then internally request.getParameterValues() method will be called.

header and headerValues

  • These two EL implicit objects are used to collect the request Headers.
  • When use header then internally request.getHeader() method will be called.
  • When use headerValues then internally request.getHeaderValues() method will be called.

Cookies

  • This EL implicit object is used to collect the request.

InitParam

  • This EL implicit object is used to collect the context parameters from the ServletContext object.

Usages:
${initParam.state}
${initParam.city}

pageContext:

  • This EL implicit object allows to access all other JSP implicit objects and their properties.

Usage:

${pageContext.session.id}
${pageContext.request.remoteAddr}
${pageContext.request.method}
${pageContext.request.requestURI}
${pageContext.response.contentType}

Scopes

  • You can store the data in different objects to access later depending on the requirement.
  • These objects are called as scope.
  • You can store the data in the required scope as an attribute.
  • An attribute is a name-value pair.
  • Name of an attribute is of type String.
  • Value of an attribute is of type Object.
  • Attribute are read-write i.e You have to store the attributes so that you can collect later.

You can use the following methods to manage the attributes.

  1. Void setAttribute(String, Object)
  2. Object getAttribute(String)
  3. void removeAttribute(String)
  4. Enumeration getAttributeNames()

Servlet Scopes

There are 3 scopes in Servlet.

  1. Request Scope
  2. Session Scope
  3. Context Scope
  • Request scope data can be accessed by a single user within the same request before delivering the response.
  • Session scope data can be accessed by single-user across multiple requests.
  • Context scope data can be accessed by multiple users across multiple requests.

JSP Scopes

There are 4 scopes in JSP.

  1. Page Scope
  2. Request Scope
  3. Session Scope
  4. Application Scope
  • Page Scope data can be accessed within the same JSP page (translated servlet).
  • Request Scope data can be accessed by a single user within the same request before delivering the response.
  • Session scope data can be accessed by single user accross multiple requests.
  • Application scope data can be accessed by multiple users across multiple requests.
  • You can store attributes directly in a request, Session, and application but you can not store attributes directly in the page because it is of type java.lang.Object.

In JSP, You can store and access the attributes using a master implicit object called pageContext.

You can use the following methods of PageContext to manage the attributes.

  1. Void setAttributes(String, Object)
  2. void setAttributes(String, Object, int)
  3. Object getAttribute(String)
  4. Object getAttribute(String, int)
  5. void removeAttribute(String)
  6. void removeAttribute(String, int);
  7. Enumeration getAttributeNameInScope(int)
  8. Object findAttribute(String)

session.setAttribute(“email”, “aashish2012@gmail.com”);
pageContext.setAttribute(“email”,”aashish2012@gmail.com”, PageContext.SESSION_SCOPE);
request.setAttribute(“email”, “aashish2012@gmail.com”);
pageContext.setAttribute(“email”,”aashish2012@gmail.com”, PageContext.REQUEST_SCOPE);
pageContext.setAttribute(“email”, “aashish2012@gmail.com”);
pageContext.setAttribute(“email”,”aashish2012@gmail.com”, PageContext.PAGE_SCOPE);

You can store the following type of data as attribute in the required scope.

  1. Strings, Wrappers, and Date
  2. Collection of Strings, Wrappers, and Date.
  3. Collection of Collections
  4. Custom or User-defined class object(Customer, Book, Student)
  5. Collection of User-defined class object
  6. Map object
  7. Collection of a Map object

1) Strings, Wrappers, and Date
In Servlet
req.setAttribute(“EM”, “aashish2012@gmail.com”);
req.setAttribute(“PH”, new Long(1234));
ctx.setAttribute(“DOB”, new Date());

In JSP
${EM}
${PH}
${DOB}
If you are not specifying the scope then it will check in all the scope if found it will display the corresponding data. First, it will check in page, request, session and then in context.
OR
${requestScope.EM}
${sessionScope.PH}
${applicationScope.DOB}

2) Custom or User defined class object(Customer)
In Servlet

class Customer
{
cid;
cname;
email;
Address address;
}
class Address
{
street;
city;
}
ses.setAttribute(“CUST”, cust);
In JSP
Cid: ${CUST.cid}
Cname:${sessionScope.Cust.email}
Email: ${sessionScope.CUST.cname}
Street: ${sessionScope.Cust.address.street}
City:${sessionScope.Cust.address.city}

3) Collection of Strings, Wrappers and Date.
In Servlet

ArrayList al = new ArrayList();
al.add(new Integer(123));
String str[] = {“dd”, “ss”…};
req.setAttribute(“AL”, al);
ses.setAttribute(“STR”, str);

In JSP
${requestScope.Al[0]}
${requestScope.Al[1]}
${requestScope.Al[0]}
${requestScope.Al[1]}

5) Collection of User defined class object
In Servlet
ArrayList al = new ArrayList();
…..
al.add(cust1);
al.add(cust2);
ses.setAttribute(“AL”, al);

In JSP
${sessionScope.AL[0]}
Cid: ${AL[0].cid}
Cname: ${session.Scope.AL[0].cname}
Email: ${sessionScope.AL[0].email}
Street : ${sessionScope.AL[0].address.street}City:${ sessionScope.AL[0].address.city}

6) Map Object
In Servlet
HashMap hm= new HashMap();
hm.put(“sid”, “11”);
hm.put(“sname”, “xadmin”);
…..
ses.setAttribute(“HM”, hm);

In JSP
Sid: ${sessionScope.HM[“sid”]}
Sname:${sessionScope.HM[“sname”]}

Conclusion: 
In this article , we learned regarding the  JSP – Expression Language (EL). This expression language is used for building the JSP page. This topic is also  important for the interview point of purpose. Please feel free to write down the comment in the comment box, if you have any doubt regarding the topic or you want to share more information about the topic. Happy Learning!

Leave a Reply

Your email address will not be published. Required fields are marked *