English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
JSP Expression Language (EL) makes it very simple to access data stored in JavaBeans. JSP EL can be used to create arithmetic expressions as well as logical expressions. In JSP EL expressions, you can use integer numbers, floating-point numbers, strings, constants true, false, and null.
Typically, when you need to specify a property value in a JSP tag, you can simply use a string:
<jsp:setProperty name="box" property="perimeter" value="100"/>
JSP EL allows you to specify an expression to represent the property value. A simple expression syntax is as follows:
${expr}
Where, expr refers to the expression. In JSP EL, the general operators are . and {} . These operators allow you to access various JavaBean properties through embedded JSP objects.
For example, the above <jsp:setProperty> tag can be rewritten in the following form using expression language:
<jsp:setProperty name="box" property="perimeter" value="${2*box.width+2*box.height"/>
When the JSP compiler encounters the "${}" format in attributes, it generates code to calculate this expression and produces a substitute to replace the value of the expression.
You can also use expression language in the template text of the tag. For example, the <jsp:text> tag simply inserts the text in its body into the JSP output:}}
<jsp:text> <h1>Hello JSP!</h1> </jsp:text>
Now, use expressions in the body of the <jsp:text> tag as follows:
<jsp:text> Box Perimeter is: ${2*box.width + 2*box.height} </jsp:text>
Parentheses can be used in EL expressions to organize sub-expressions. For example, ${(1 + 2) * 3equals9However, ${1 + (2 * 3equals7.
To disable the evaluation of EL expressions, you need to use the page directive to set the isELIgnored property value to true:
<%@ page isELIgnored ="true|false" %>
In this way, the EL expression will be ignored. If set to false, the container will calculate the EL expression.
EL expressions support most of the arithmetic and logical operators provided by Java:
Operator | Description |
---|---|
. | Access a Bean property or a map entry |
[] | Access an array or list element |
( ) | Organize a sub-expression to change the priority |
+ | Addition |
- | Subtraction or negative |
* | Multiplication |
/ or div | Division |
% or mod | Modulo |
== or eq | Test if it is equal |
!= or ne | Test if it is not equal |
< or lt | Test if it is less than |
> or gt | Test if it is greater than |
<= or le | Test if it is less than or equal to |
>= or ge | Test if it is greater than or equal to |
&& or and | Test logical AND |
|| or or | Test logical OR |
! or not | Test negation |
empty | Test if it is a null value |
JSP EL allows you to use functions in expressions. These functions must be defined in custom tag libraries. The syntax for using functions is as follows:
${ns:func(param1, param2, ...)}
ns refers to the namespace (namespace), func refers to the name of the function, param1It refers to the first parameter, param2It refers to the second parameter, and so on. For example, there is a function fn:length defined in the JSTL library, which can be used to get the length of a string as follows:
${fn:length("Get my length")}
To use any function from a tag library, you need to install these libraries on the server and then include these libraries in JSP files using the <taglib> tag.
The following implicit objects are supported under JSP EL:
Implicit objects | Description |
---|---|
pageScope | Page scope |
requestScope | Request scope |
sessionScope | Session scope |
applicationScope | Application scope |
param | Parameters of the Request object, string |
paramValues | Parameters of the Request object, string collection |
header | HTTP information headers, string |
headerValues | HTTP information headers, string collection |
initParam | Context initialization parameters |
cookie | Cookie values |
pageContext | The pageContext of the current page |
You can use these objects in expressions as you would use variables. The following examples will be given to better understand this concept.
The pageContext object is a reference to the JSP pageContext object. Through the pageContext object, you can access the request object. For example, to access the query string passed to the request object, you can do so as follows:
${pageContext.request.queryString}
The variables pageScope, requestScope, sessionScope, and applicationScope are used to access variables stored at various scope levels.
For example, if you need to explicitly access the box variable at the applicationScope level, you can access it as follows: applicationScope.box.
The param and paramValues objects are used to access parameter values, through the use of the request.getParameter and request.getParameterValues methods.
For example, to access a parameter named order, you can use the expression: ${param.order}, or ${param["order"]}.
The following example demonstrates how to access the username parameter in the 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, while the paramValues object returns an array of strings.
The header and headerValues objects are used to access header information by using the request.getHeader method and the request.getHeaders method.
For example, to access a header named user-agent header information can be used as follows: ${header.user-agent}, or ${header["user-agent"]}。
The following example shows how to access user-User agent header information:
<%@ 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 running result is as follows:
The header object returns a single value, while headerValues returns an array of strings.