0% found this document useful (0 votes)
219 views25 pages

Expression Language (EL) in JSP

The document discusses Expression Language (EL) in JSP. EL was added in JSP 2.0 to allow for scriptless JSP pages. EL simplifies accessing data stored in Java beans and implicit objects like request and session. Syntax uses ${expression} and can perform operations, access objects and their properties, and evaluate conditions. Common implicit objects include pageScope, requestScope, sessionScope, param, and header.

Uploaded by

Ankit Agrawal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
219 views25 pages

Expression Language (EL) in JSP

The document discusses Expression Language (EL) in JSP. EL was added in JSP 2.0 to allow for scriptless JSP pages. EL simplifies accessing data stored in Java beans and implicit objects like request and session. Syntax uses ${expression} and can perform operations, access objects and their properties, and evaluate conditions. Common implicit objects include pageScope, requestScope, sessionScope, param, and header.

Uploaded by

Ankit Agrawal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

Expression Language (EL) in JSP

• Expression Language(EL) was added to JSP 2.0


specification. The purpose of EL is to produce
scriptless JSP pages.
• Uses:--
• Use shorthand syntax to access and output
object properties. Usually used in
• conjunction with beans and MVC.
• The Expression Language (EL) simplifies the
accessibility of data stored in the Java Bean
component, and other objects like request,
session, application etc.
• There are many implicit objects, operators and
reserve words in EL.
• Syntax for Expression Language (EL)
• ${ expression }
Example
Arithmetic operation

• <html>
• <body>
• ${1<2}
• ${1+2+3}
• </body>
EXAMPLE
• <% pageContext.setAttribute("pageColor", "blue"); %>

• <body bgcolor="${pageScope.pageColor}">

• <h1>Welcome to the ${param.department} Department</h1>

• Here are some basic comparisons:


• <p>
• Is 1 less than 2? ${1<2} <br>
• Does 5 equal 5? ${5==5} <br>
• Is 6 greater than 7? ${6 gt 7}<br>

• <p>Now for some math:<br>


• 6 + 7 = ${6+7}<br>
• 8 x 9 = ${8*9}<br>

• <hr>You appear to be using the following browser:


• ${header["user-agent"]}
Implicit Objects in Expression Language (EL)
Implicit Objects Usage
it maps the given attribute name with the value set in the page
pageScope
scope
it maps the given attribute name with the value set in the request
requestScope
scope
it maps the given attribute name with the value set in the session
sessionScope
scope
it maps the given attribute name with the value set in the
applicationScope
application scope
param it maps the request parameter to the single value
paramValues it maps the request parameter to an array of values
header it maps the request header name to the single value
headerValues it maps the request header name to an array of values
cookie it maps the given cookie name to the cookie value
initParam it maps the initialization parameter
How EL expression is use
• EL expression can be used in two ways in a JSP
page
• As attribute values in standard and custom
tags. Example
• < jsp:include page="${location}" >
• As a output with HTML tag
• <h1>Welcome ${name}</h1>
• index.jsp

• <form action="process.jsp">
• Enter Name:<input type="text" name="name" />
• <input type="submit" >
• </form>
To reterive
• Welcome, ${ param.name }
• Example of Expression Language that prints
the value set in the session scope

<% session.setAttribute("user",“cdac123"); %>

<a href="process.jsp">visit</a>
To reterieve value
• Value is ${ sessionScope.user }
Reserve words in EL
lt le gt ge

eq ne true false

and or not instanceof

div mod empty null


Evaluating Expressions Conditionally

• ${ test ? expression1 : expression2 }


Accessing Bean
• <jsp:useBean
• id="customer"
• class="coreservlets.NameBean"
• scope="request, session, or application" />

• <jsp:getProperty name="customer"
property="firstName" />
• ${customer.firstname}
• Deactivating the Expression
• Language in Individual JSP Pages

• <%@ page isELEnabled="false" %>


• Escaping Special Characters
• If you want ${ to appear in the page output,
use \${ in the JSP page.
• If you want to
• use a single or double quote within an EL
expression, use \' and \", respectively.
MCQ
• What gets printed when the following
expression is evaluated? Select the one
correct answer.

${(1==2) ? 4 : 5}
• 1
• 2
• 4
• 5
• What gets printed when the following
expression is evaluated? Select the one
correct answer.

${4 div 5}
• 0
• 0.8
• 1
• -1
• What gets printed when the following
expression is evaluated? Select the one
correct answer.

${12 % 4}
• 0
• 3
• 8
• 16
• What is the effect of executing the following JSP statement,
assuming a class with name Employee exists in classes package.
<%@ page import = "classes.Employee" %>
• <jsp:useBean id="employee" class="classes.Employee"
scope="session"/> <jsp:setProperty name="employee"
property="*"/>

1. The code does not compile as there is no property attribute of


setProperty tag.
2. The code does not compile as property attribute cannot take * as a
value.
3. The code sets value of all properties of employee bean to "*".
4. The code sets the values of all properties of employee bean to
matrching parameters in request object.
• What is the effect of evaluation of following
expression? Select the one correct answer.

${(5*5) ne 25}
• true
• false
• 25
• The expression does not compile as ne is not a
valid operator.
Q What is the effect of evaluation of following expression? Select the one correct
answer.
${'cat' gt 'cap'}
true
• false
• catcap
• The expression does not compile as gt operator cannot be applied on strings.

• ++++++++++++++

Q How many numbers are printed, when the following JSTL code fragment is
executed? Select the one correct answer.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:forEach var="item" begin="0" end="10" step="2">
${item}
</c:forEach>
1
• 5
• 6
• 11
• Which of these represent the correct path for
the core JSTL library in JSTL version 1.1? Select
the one correct answer.
http://java.sun.com/jsp/jstl/core
• http://java.sun.com/jsp/core
• http://java.sun.com/core
• http://java.sun.com/jsp/jstl1.1/core

You might also like