Professional Documents
Culture Documents
EL (Expression Language)
EL (Expression Language)
(Expression Language)
Contents
1 Need for EL
2 Solution!!!!
3 EL Syntax
4 Implicit Objects
5 Using “.” Operator
6 Using “[ ]” operator
7 Important points
8 EL Operators
9 EL reserved words
10 EL Functions
11 Creating a class
12 TLD file
13 Use in JSP file
Know
• What EL is
• What EL implicit objects are
• What are EL functions
• How to enable/disable EL
• How to make JSP strictly scriplessd
Be Able To
• Write scriptless JSP using EL
Scriptless JSP
• A JSP page without any scriptlets is a
script-less JSP.
• Our attempt is to reduce the clutter of java
code in the JSP
• With standard jsp action tags we can
achieve some script-less-ness.
– But surely this is not enough.
– For instance how will you print request
parameters in a script-less way?
Need for EL
Take another example-
SO WE CONCLUDE
STANDARD ACTIONS ARE NOT ENOUGH TO MAKE JSP
SCRIPTLESS
EL to rescue!
• Using EL to solve the problem
<body>
<jsp:useBean id=“car” class=“Car”
scope=“request” />
${car.engine.engine_type}
</body>
instead of <jsp:getProperty>
${E1.E2}
Displays
Mr. Rama 402
EL Implicit Objects
• pageScope
•Note that these objects are
• requestScope not the same as JSP implicit
• sessionScope object
• applicationScope
• param
• Map objects
paramValues
• header
• headerValues
• cookie
• initParam
• pageContext Like JSP implicit object
Using “.” Operator
• When the variable is on the left side of the dot, it
is either a Map(Key/Value pair) or a
bean(properties)
${E1.E2}
Key property
Examples
• We have seen an example for java bean with
property
<jsp:useBean class=“c.Emp” id=“a”/>
${a.name}
• Map Example
<jsp:useBean class=“c.Emp” id=“a”/>
${pageScope.a.name}
– Where ‘pageScope’ is implicit object of map
type.
Using “[ ]” operator
• When the variable is on the left side of the [], it
can be
Map
bean
List
array
${E1[“E2”]}
Map(java.util.Map)
array
Key
index
property
index
request.setAttribute("carmap",carmap);
request.setAttribute("cartype",cartype);
request.setAttribute("size","Medium");
request.getRequestDispatcher("i2.jsp").forwa
rd(request,response);
}
In JSP
<body>
My car is: ${carmap.Large}
<br> or <br>
Both print Mercedes
My car is: ${carmap["Large"]}
<br>
prints Large
${pageContext.session.id}
EL implicit object
Question?
• Suppose that there is a ‘size’ attribute set in the
session scope as well. Write an EL Expression
to print the size attribute in the session scope?
${pageContext.session.size}
${sessionScope.size}
Getting other parameters
• ${cookie.uname.valu}
– Returns the value of uname cookie
• ${initParam.color}
• <%=application.getInitParameter(“c
olor”)%>
• Logical Operators
– && , and AND
– || , or OR
– !, not NOT
In Logical expressions (using logical operators
and/or relational operator, EL treats the
unknown variable (null value) as false.
EL reserved words
Note:
${empty obj} returns true if obj is null or
empty.
Examples
xsi:schemaLocation=“http://java.sun.com/xml/ns/
j2ee/web-jsptaglibrary_2_0.xsd” version=“2.0”>
<tlib-version> 1.2</tlib-version>
<uri>CountFunction</uri>
<function>
<name>counter</name>
<function-class>Counter</function-class>
<function-signature> int
countIt()</function-signature> WEB-INF/ my.tld
</function></tablib>
Use in JSP file
<%@ taglib prefix=“mycnt”
uri=“CountFunction”%>
<html><body>
${mycnt:counter()}
</body></html>
</jsp-property-group>
</jsp-config>
. . .
</web-app>
Ensuring that JSPs are scriptless
<web-app>
…
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp </url-pattern>
<scripting-invalid>
true
</scripting-invalid>
</jsp-property-group>
</jsp-config>
</web-app>