Expression Language (EL) in JSP
JSP EL Overview :
Expression language (EL) has been introduced in JSP 2.0. The main purpose of it to simplify the process of accessing data from bean properties and from implicit objects.By using JSP Expression Language you can get data from javaBeans,maps,arrays and lists that have been stored as attributes of a web application.
You can use EL instead of scriptlet.Expression Language is simpler and easy to understand.
Advantage of EL :
- EL has more compact syntax than Standard JSP tag.
- EL lets you access nested properties
- EL lets you access collections such as maps,arrays,and lists.
JSP EL Overview :
Expression language (EL) has been introduced in JSP 2.0. The main purpose of it to simplify the process of accessing data from bean properties and from implicit objects.By using JSP Expression Language you can get data from javaBeans,maps,arrays and lists that have been stored as attributes of a web application.
You can use EL instead of scriptlet.Expression Language is simpler and easy to understand.
Advantage of EL :
- EL has more compact syntax than Standard JSP tag.
- EL lets you access nested properties
- EL lets you access collections such as maps,arrays,and lists.
How to use EL in a JSP Page :
In a JSP by default scripting elements are enabled and EL statement/expressions are disabled.
To enable the EL expression in JSP, you need to use following page directive.
<%@ page isELIgnored="false"%>
Syntax for Expression Language (EL) :
${ expr }
In the above syntax, expr is an expression. When the Java compiler sees the sign ${}, it evaluates the expr and injects the result in the place where ${expr} is called.
JSP EL Implicit Objects :
JSP Expression Language provides many implicit objects that we can use to get attributes from different scopes and parameter values. The list is given below.
JSP EL Implicit Objects | Type | Description |
---|---|---|
pageScope | Map | A map that contains the attributes set with page scope. |
requestScope | Map | Used to get the attribute value of request scope. |
sessionScope | Map | Used to get the attribute value of session scope. |
applicationScope | Map | Used to get the attributes value from application scope. |
param | Map | Used to get the request parameter value/td> |
paramValues | Map | Used to get the request param values in an array, useful when request parameter contain multiple values. |
header | Map | Used to get request header information. |
headerValues | Map | Used to get header values in an array. |
cookie | Map | Used to get the cookie value in the JSP |
initParam | Map | Used to get the context init params, we can’t use it for servlet init params |
pageContext | pageContext | Same as JSP implicit pageContext object, used to get the request, session references etc. example usage is getting request HTTP Method name. |
Sample example of EL that prints name of the user :
In this example, we have created two files index.jsp and info.jsp. The index.jsp file gets input from the user and sends the request to the info.jsp which will prints the name of the user using EL.
1) index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>JSP EL Example</title> </head> <body> <form action="info.jsp"> Enter Name:<input type="text" name="name" /><br/><br/> <input type="submit" value="go"/> </form> </body> </html>
2) info.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>JSP EL Example</title> </head> <body> Welcome, ${ param.name } </body> </html>
Output in Browser:
Enter the value and click on "go" you will get value of name in "info.jsp" as shown below
2 : Example of EL that prints the value set in the application Scope :
In this example we have set the attributes using application implicit object in "index.jsp" and on the "info.jsp" page we have got those attributes using application Scope of Expression language.
1) index.jsp
<html> <head> <title>EL Example</title> </head> <body> <% application.setAttribute("name", "Mukesh"); application.setAttribute("phone", "1234567"); %> <a href="info.jsp">Submit</a> </body> </html>
2) info.jsp
<html> <head> <title>Display Page</title> </head> <body> ${applicationScope.name} <br> ${applicationScope.phone} </body> </html>
Output in Browser :
Getting the Properties of the Bean using EL :
In this example we will discuss how we will get java Bean properties using EL.
Example without EL :
1)Topic.java
package com.example; public class Topic { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
2)index.jsp
<html> <head> <title>jsp:useBean Demo</title> </head> <body> <jsp:useBean id="topic" class="com.example.Topic" /> <jsp:setProperty name="topic" property="name" value="JSP UseBean Example" /> <h1> <jsp:getProperty name="topic" property="name" /> </h1> </body> </html>
Same Example with EL :
1)Topic.java
package com.example; public class Topic { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
2)index.jsp
<html> <head> <title>jsp:useBean Demo</title> </head> <body> <jsp:useBean id="topic" class="com.example.Topic" /> <jsp:setProperty name="topic" property="name" value="JSP UseBean Example" /> <h1>${topic.name}</h1> </body> </html>
Output in Browser :
Related Articles