wellnwill

EL

Introduction to Expression Language

The Expression Language (EL) simplifies the data accessibility from various type of application objects like Java Beans, request, session and application etc. There are many implicit objects, operators and reserve words in EL. It is the newly added feature in JSP technology version 2.0.

Syntax

${expression}
${EL.Statement}
${attrName}
${attrName[index]}
${OneObject.MemeberObject.PropertyNameOfMemberObject}


EL implicit object

  • param
  • paramValues
  • header
  • headerValues
  • cookie
  • pageScope
  • requestScope
  • session’scope
  • applicationScope
  • initParam
  • pageContext(*)

param and paramValues

These two EL implicit objects are used to collect the request parameters and its values When param is used then internally request.getParameter() method will be called When paramValues is user then internally request.getParameterValues() will be called

Usage:

${param.name}
${param.course}
${paramValues.course[0]}
${ paramValues.course[1]}


header and header values

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

Usage:

${header.host}
${header.referer}
${headrValues.accept-language[0]}
${headrValues.accept-language[1]}


This EL implicit object is used to collect the request cookies

Usage:

${cookies.cookieName.value}
${cookie.email.value}

Using Java Code Using EL
Cookie ck[] = request.getCookies();
for(Cookie c: ck){
        String cName = c.getName();
    if(cName.equals("email")){
            String cValue = c.getValue();
            out.println(cValue);
            break;
        }
}
${cookie.email.value}

initParam

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

Usage:

${initParam.institute}
${initParam.website}


pageContext

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

Usage:

${pageContext.session.id}
${pageContext.session.length}
${pageContext.request.removeAddr}
${pageContext.request.method}
${pageContext.request.requestURL}
${pageContext.request.contentType}


pageScope, requestScope, sessionScope, applicationScope

  • In servlet class, you can store the data as attribute
  • In JSP, you can collect the attribute data and display that data
  • You can store the following type of data as attribute in the required scope
    1. String, Wrappers and Date
    2. Collection of String, Wrappers and Date(****JSTL)
    3. Collection of Collections(****JSTL)
    4. User defined objects eg: Student, User, Book
    5. Collection of user defined object
    6. Map object
    7. Collection of Map objects(****JSTL)

EL - String, Wrappers and Date

Set attributes in ServletGet attributes values in JSP
req.setAttribute("name", "Atul");
ses.setAttribute("userName", "AtulDwivedi");
ctx.setAttribute("joiningData", new Date());
${requestScope.name}
${sessionScope.userName}
${applicationScope.joiningDate}

EL - Collection of String, Wrappers and Date

Set attributes in ServletGet attributes values in JSP
List modules = new ArrayList();
modules.add("Java Module 2");
modules.add("Java Module 3");
 

session.setAttribute(“MOD”, modules);
${sessionScope.MOD[0]}
${sessionScope.MOD[1]}
/**
 * Using index directly is not good practice,
 * this can be removed using JSTL.
 */

User defined objects

Set attributes in ServletGet attributes values in JSP
class Course{
 String courseName;
 String trainerName;
 ...
}

class Trainee{
 String traineeName;
 Course course;
 ...
}
session.setAttribute("TRAINEE", trainee);
Trainee Name : ${sessionScope.TRAINEE.traineeName}
 
Course Name:
${sessionScope.TRAINEE.course.courseName}

Collection of user defined objects

Set attributes in ServletGet attributes values in JSP
class Course{
 String courseName;
 String trainerName;
 ...
}
 
class Trainee{
 String traineeName;
 Course course;
 ...
}
 
List traineeList = new ArrayList();
traineeList.add(trainee1);
traineeList.add(trainee2);

session.setAttribute("TRNLIST", traineeList);
Trainee Name :
${sessionScope.TRNLIST[0].traineeName}
 
Course Name:
${sessionScope. TRNLIST[0].course.courseName}

Map objects

Set attributes in ServletGet attributes values in JSP
Map map = new HashMap();
map.put(“traineeName”, “Shree”);
map.put(“courseName”, “Java”);
…
session.setAttribute("MAP", map);
Trainee Name : 
${sessionScope.MAP[“traineeName”] }
 
Course Name:
${sessionScope. MAP[“courseName”] }

Collection of maps

Set attributes in ServletGet attributes values in JSP
Map trnMap = new HashMap();
map.put(“traineeName”, “Shree”);
…
Map courseMap = new HashMap();
map.put(“courseName”, “Java”);
…
Map map = new HashMap();
map.put(“map1”, trnMap);
map.put(“map2”, courseMap);
…
 
session.setAttribute("MAP", map);
Trainee Name : ${sessionScope.MAP[0].[“ traineeName”] }
 
Course Name:
${sessionScope.MAP[1].[“ courseName”] }
 
Footer with Map

Top