May 12, 2021 JSP
The JSP Expression Language (EL) makes it easy to access data stored in JavaBean. J SP EL can be used to create both arithmetic expressions and logical expressions. Integers, floats, strings, constants true, false, and null can be used within the JSP EL expression.
Typically, when you need to specify a property value in a JSP label, you simply use a string:
<jsp:setProperty name="box" property="perimeter" value="100"/>
JSP EL allows you to specify an expression to represent a property value. A simple expression syntax is as follows:
${expr}
Where
expr
expressions. T
he common operators in JSP EL are
"."
and
"[]"
These two operators allow you to access a wide variety of JavaBean properties through
JavaBean
For example, the above label,
<jsp:setProperty>
rewritten in the expression language as follows:
<jsp:setProperty name="box" property="perimeter" value="${2*box.width+2*box.height}"/>
When the JSP compiler sees
${}
in the property, it generates code to evaluate the expression and produces an alternative to the value of the expression.
You can also use expression language in the template text of a label.
For
<jsp:text>
text from its body into the JSP output:
<jsp:text>
<h1>Hello JSP!</h1>
</jsp:text>
Now, use the expression in the body of the
<jsp:text>
like this:
<jsp:text>
Box Perimeter is: ${2*box.width + 2*box.height}
</jsp:text>
You can use parentheses to organize subexpressions in EL expressions.
For
${(1 + 2) * 3}
is equal to 9,
${1 + (2 * 3)}
to 7.
To deactivate the evaluation of an EL expression, you need to
page
instruction to set the
isELIgnored
property value
true
<%@ page isELIgnored ="true|false" %>
In this way, the EL expression is ignored.
If set to
false
the container evaluates the EL expression.
EL expressions support arithmetic and logic operators provided by most Javas:
Operator | Describe |
---|---|
. |
Access a
Bean
or a mapping entry
|
[] | Access elements of an array or list |
( ) | Organize a subexpression to change priority |
+ | Add |
- | minus or negative |
* | By |
/ or div | Except |
% or mod | Molding |
== or eq | Test for equality |
!= or ne | The test is not equal |
< or lt | Test whether it is less than |
> or gt | Test whether it is greater than |
<= or le | Test whether it is less than or equal |
>= or ge | Test whether it is greater than or equal to |
&& or and | Test logic with |
|| or or | Test logic or |
! or not | The test reverses |
empty | Test whether the value is empty |
JSP EL allows you to use functions in expressions. T hese functions must be defined in a custom label library. The syntax for using functions is as follows:
${ns:func(param1, param2, ...)}
ns refers to namespace,
func
refers to the name of the function,
param1
refers to the first argument,
param2
refers to the second argument, and so on. F
or example, there is
fn:length
defined in the JSTL library, that can get the length of a string like this:
${fn:length("Get my length")}
To use functions in any label library, you need to install them on the
<taglib>
and then include them in the JSP file using the hashtag.
JSP EL supports the implied objects listed in the following table:
The implied object | Describe |
---|---|
pageScope | Page scope |
requestScope | Request scope |
sessionScope | Session scope |
applicationScope | Application scope |
param | The argument to the Request object, the string |
paramValues | The parameters of the Request object, the collection of strings |
header | HTTP header, string |
headerValues | HTTP header, string collection |
initParam | The context initializes the parameters |
cookie | The value of the cookie |
pageContext | PageContext for the current page |
You can use these objects in expressions as if you were using variables. Here are a few examples to better understand the concept.
pageContext
object is a reference to the
pageContext
JSP. T
he
pageContext
allows you to access
request
object.
For example, access
request
string passed in by the request object, as this is:
${pageContext.request.queryString}
pageScope
requestScope
sessionScope
applicationScope
to access variables stored at various scope levels.
For example, if you need explicit access to a box variable at the
applicationScope
layer, you can access it
applicationScope.box
box
param
and
paramValues
to access parameter values by
request.getParameter
method
request.getParameterValues
method.
For example, to access an argument
order
you can use the expression as
${param.order}
or
${param["order"]}
The following example shows how to access the
username
request:
<%@ page import="java.io.*,java.util.*" %>
<% String title = "Accessing Request Param"; %>
<html>
<head>
<title><% out.print(title); %></title>
</head>
<body>
<center>
<h1><% out.print(title); %></h1>
</center>
<div align="center">
<p>${param["username"]}</p>
</div>
</body>
</html>
The param object returns a single string,
paramValues
object returns an array of strings.
header
headerValues
to access the information header by
request.getHeader
request.getHeaders
method.
For example, to access an information header
user-agent
you can use an expression like
${header.user-agent}
${header["user-agent"]}
The following example shows how to
user-agent
header:
<%@ page import="java.io.*,java.util.*" %>
<% String title = "User Agent Example"; %>
<html>
<head>
<title><% out.print(title); %></title>
</head>
<body>
<center>
<h1><% out.print(title); %></h1>
</center>
<div align="center">
<p>${header["user-agent"]}</p>
</div>
</body>
</html>
The results are as follows:
The header object returns a single value, while the headerValues returns an array of strings.