JSP expression language

JSP Expression Language (EL) makes it easy to access data stored in JavaBeans. JSP EL can be used to create both arithmetic expressions and logical expressions. You can use integers, floats, strings, constants true, false, and null in JSP EL expressions.

A simple syntax

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 attribute values. A simple expression syntax is as follows:


Among them, expr refers to an expression. The common operators in JSP EL are . and {} . These two operators allow you to access various JavaBean properties through the embedded JSP object.

For example, the above <jsp:setProperty> Tags can be rewritten using the expression language as follows:

<jsp:setProperty name="box" property="perimeter" 

When the JSP compiler sees the "$ {}" format in the attribute, it will generate code to evaluate the expression and produce a replacement to replace the value of the expression.

You can also use an expression language in the template text of a label. such as <jsp:text>The tag simply inserts the text from its body into the JSP output:

<h1>Hello JSP!</h1>

now at<jsp:text>Use expressions in the body of the tag, like this:

Box Perimeter is: ${2*box.width + 2*box.height}

Parentheses can be used in EL expressions to organize subexpressions. For example $ {(1 + 2) * 3} is equal to 9, but $ {1 + (2 * 3)} is equal to 7.

To disable the evaluation of EL expressions, you need to use the page directive to set the value of the isELIgnored attribute to true:

<%@ page isELIgnored ="true|false" %>

In this way, the EL expression is ignored. If set to false, the container will evaluate the EL expression.

Basic operators in EL

EL expressions support most of the arithmetic and logical operators provided by Java:

Operator description
. Access a bean property or a mapping entry
[] Accessing elements of an array or linked list
( ) Organize a sub-expression to change the priority
+ plus
- Minus or negative
* Multiply
/ or div except
% or mod Demo
== or eq Test for equality
!= or ne Tests are not waiting
< or lt Test if is less than
> or gt Test if greater than
<= or le Test if is less than or equal to
>= or ge Test if greater than or equal
&& or and Test logic and
|| or or Test logic OR
! or not Test reversal
empty Test for null value

Functions in JSP EL

JSP EL allows you to use functions in expressions. These functions must be defined in a custom tag library. The syntax of the function is as follows:

${ns:func(param1, param2, ...)}

ns refers to the namespace, func refers to the name of the function, param1 refers to the first parameter, param2 refers to the second parameter, and so on. For example, there is a function fn: length, which is defined in the JSTL library. You can get the length of a string as follows:

${fn:length("Get my length")}

To use functions from any tag library, you need to install those libraries in the server and then use <taglib> The tag includes these libraries in a JSP file.

JSP EL Hidden Objects

JSP EL supports the implicit objects listed in the following table:

Hidden objects Description
pageScope page scope
requestScope request scope
sessionScope session scope
applicationScope application scope
param Parameter of Request object, string
paramValues ​​ Request object parameters, a collection of strings
header HTTP header, string
headerValues ​​ HTTP headers, string collections
initParam Context initialization parameters
cookie Cookie value
pageContext PageContext of the current page

You can use these objects in expressions just as you would use variables. A few examples are given below to better understand this concept.

pageContext object

The pageContext object is a reference to the pageContext object in the JSP. The pageContext object gives you access to the request object. For example, access the query string passed in the request object, like this:

  $   {  pageContext  .   request  .   queryString  }   

Scope object

The pageScope, requestScope, sessionScope, and applicationScope variables are used to access variables stored at various scope levels.

For example, if you need to explicitly access the box variable in the applicationScope layer, you can access it like this: applicationScope.box.

param and paramValues ​​objects

The param and paramValues ​​objects are used to access parameter values ​​by using the request.getParameter method and request.getParameterValues ​​method.

For example, to access a parameter named order, you can use an expression like this: $ {param.order}, or $ {param ["order"]}.

The following example shows how to access the username parameter in the request:

<%@ page import="java.io.*,java.util.*" %>
    String title = "Accessing Request Param";
<title><% out.print(title); %></title>
<h1><% out.print(title); %></h1>
<div align="center">

The param object returns a single string, while the paramValues object returns an array of strings.

header and headerValues objects

The header and headerValues objects are used to access information headers by using the request.getHeader method and request.getHeaders method.

For example, to access a header named user-agent, you can use the expression: $ {header.user-agent}, or $ {header ["user-agent"]}.

The following example shows how to access the user-agent header:

<%@ page import="java.io.*,java.util.*" %>
    String title = "User Agent Example";
<title><% out.print(title); %></title>
<h1><% out.print(title); %></h1>
<div align="center">