JSP Expression Language (EL) makes accessing data stored in JavaBeans very simple. JSP EL can be used to create both arithmetic and logical expressions. Integers, floating point numbers, strings, constants true, false, and null can be used in JSP EL expressions.
Typically, when you need to specify an attribute value in a JSP tag, 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}Among them,
expr refers to an expression. The common operators in JSP EL are
"."and
"[]". These two operators allow you to access a variety of
JavaBeans Properties.
For example, the above
<jsp:setProperty>Tags can be rewritten using the expression language as follows:
<jsp:setProperty name="box" property="perimeter" value="${2*box.width+2*box.height}"/>When the JSP compiler sees "
${} "After formatting, it generates code to evaluate the expression and produce a substitute for the expression's value.You can also use expression languages in the label's template text. For example
<jsp:text>The tag simply inserts the text in its body into the JSP output:
<jsp:text><h1>Hello JSP!</h1></jsp:text>
now, in
<jsp:text>Use an expression in the body of the tag, like this:
<jsp:text>Box Perimeter is: ${2*box.width + 2*box.height}</jsp:text>Parentheses can be used to organize subexpressions in EL expressions. For example
${(1 + 2) * 3} equals 9, but ${1 + (2 * 3)} Equal to 7.To disable the evaluation of EL expressions, use
pageInstructions will
isELIgnoredThe attribute value is set to
true:
<%@ page isELIgnored ="true|false" %>
This way, EL expressions are ignored. If set to
false, the container will evaluate the EL expression.
EL expressions support most of the arithmetic and logical operators provided by Java:
| Operator | describe |
|---|---|
| . | Visit aBeanAttribute or a map entry |
| [] | Access elements of an array or linked list |
| ( ) | Organize a subexpression to change precedence |
| + | add |
| - | minus or negative |
| * | take |
| / or div | remove |
| % or mod | Take a mold |
| == or eq | Test for equality |
| != or ne | Test if unequal |
| < or lt | Test if less than |
| > or gt | Test whether greater than |
| <= or le | Test if less than or equal to |
| >= orge | Test if greater than or equal to |
| && or and | Test logical AND |
| || or or | Test logical OR |
| ! or not | test negation |
| empty | Test for null value |
JSP EL allows you to use functions in expressions. These functions must be defined in the custom tag library. The syntax for using the function is as follows:
${ns:func(param1, param2, ...)}ns refers to the namespace,
funcRefers to the name of the function,
param1Refers to the first parameter,
param2 refers to the second parameter, and so on. For example, there is a function
fn: length, defined in the JSTL library, can be used to get the length of a string as follows:
${fn:length("Get my length")}To use functions from any tag library, you need to install these libraries in the server and then use
<taglib>Tags include these libraries in JSP files.
JSP EL supports the implicit objects listed in the following table:
| Hidden object | describe |
|---|---|
| pageScope | page scope |
| requestScope | request scope |
| sessionScope | session scope |
| applicationScope | application scope |
| param | Parameters of Request object, string |
| paramValues | Parameters of Request object, string collection |
| header | HTTP header, string |
| headerValues | HTTP header, string collection |
| initParam | Context initialization parameters |
| cookies | Cookie value |
| pageContext | pageContext of the current page |
You can use these objects in expressions just like variables. Next, several examples will be given to better understand this concept.
pageContextThe object is in JSP
pageContextObject reference. Pass
pageContextObject that you can access
requestObject. For example, visit
requestThe query string passed in by the object, like this:
${pageContext.request.queryString}
pageScope,
requestScope,
sessionScope,
applicationScopeVariables are used to access variables stored at various scope levels.
For example, if you need to explicitly access the
applicationScopeLayered
boxVariables can be accessed like this:
applicationScope.box.
paramand
paramValuesObject used to access parameter values, by using
request.getParameterMethods and
request.getParameterValuesMethod.
For example, accessing a file named
order parameters, you can use expressions like this:
${param.order} , or ${param["order"]} .The following example shows how to access the request in
usernameParameters:
<%@ 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
paramValuesObject returns an array of strings.
headerand
headerValuesObject is used to access the information header, by using
request.getHeaderMethods and
request.getHeadersMethod.
For example, to access a file named
user-agent information header, you can use the expression like this:
${header.user-agent} , or ${header["user-agent"]} .The following example shows how to access
user-agentInformation 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 running results are as follows:

The header object returns a single value, while headerValues returns an array of strings.