Search This Blog

Thursday 9 January 2014

Expression Language (EL) in JSP

Expression Language (EL) in JSP


There are many implicit objects, operators and reserve words in EL.The Expression Language (EL) simplifies the accessibility of data stored in the Java Bean component, and other objects like request, session, application etc.
It is the newly added feature in JSP technology version 2.0.
EL helps you to save your programming time.

Syntax for Expression Language (EL)

  1. ${ expression }  

Implicit Objects in Expression Language (EL)

There are many implicit objects in the Expression Language. They are as follows:
Implicit ObjectsUsage
pageScopeit maps the given attribute name with the value set in the page scope
requestScopeit maps the given attribute name with the value set in the request scope
sessionScopeit maps the given attribute name with the value set in the session scope
applicationScopeit maps the given attribute name with the value set in the application scope
paramit maps the request parameter to the single value
paramValuesit maps the request parameter to an array of values
headerit maps the request header name to the single value
headerValuesit maps the request header name to an array of values
cookieit maps the given cookie name to the cookie value
initParamit maps the initialization parameter
pageContextit provides access to many objects request, session etc.

Simple example of Expression Language that prints the name of the user

In this example, we have created two files index.jsp and process.jsp. The index.jsp file gets input from the user and sends the request to the process.jsp which in turn prints the name of the user using EL.

index.jsp

  1. <form action="process.jsp">  
  2. Enter Name:<input type="text" name="name" /><br/><br/>  
  3. <input type="submit" value="go"/>  
  4. </form>  

process.jsp

  1. Welcome, ${ param.name }  

Example of Expression Language that prints the value set in the session scope

In this example, we printing the data stored in the session scope using EL. For this purpose, we have used sessionScope object.

index.jsp

  1. <h3>welcome to index page</h3>  
  2. <%  
  3. session.setAttribute("user","sonoo");  
  4. %>  
  5.   
  6. <a href="process.jsp">visit</a>  

process.jsp

  1. Value is ${ sessionScope.user }  

No comments:

Post a Comment