JSP Expression Language


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.


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

${expr}

where expr refers to the expression. Common operators in JSP EL are "." and "[]". These two operators allow you to access a variety of JavaBean properties through embedded JSP objects.

For example, the above <jsp:setProperty> tag can be rewritten into the following form using expression language:

<jsp:setProperty name="box" property="perimeter" 
                 value="${2*box.width+2*box.height}"/>

When the JSP compiler sees "${}" in the property 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 template text of labels. 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 an expression in the body of the <jsp:text> tag, like this:

<jsp:text>
Box Perimeter is: ${2*box.width + 2*box.height}
</jsp:text>

Parents 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.

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

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

In this way, the EL expression will be 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:

OperatorDescription
              .               Access a Bean property or a mapping entry
            []               Access elements of an array or linked list
            ( )             Structure a subexpression to change precedence
            +             add
-               Subtract or negative
            *             take
/ or div               remove
% or mod               Modulo
            == or eq               Test for equality
            != or ne               Test whether it 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 to
            >= or ge             Test whether it is greater than or equal to
              && or and             Test logic and
            || or or               Test logic or
            ! or not               Test negation
            empty               Test whether the value is empty

Functions in JSP EL

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, func refers to the name of the function, param1 refers to the first parameter, param2 refers to the second parameter, and And so on. For example, there is the function fn:length, defined in the JSTL library, which can be used to obtain the length of a string as follows:

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

To use the functions in any tag library, you need to install these libraries on the server , and then include these libraries in the JSP file using the <taglib> tag.


JSP EL implicit objects

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

## pageScope               page scope             requestScope               request scope             sessionScope               session scope             applicationScope               application scope             param               Parameters of the Request object, string             paramValues               Parameters of Request object, string collection             header             HTTP header, string               headerValues               HTTP header, string set             initParam               Context initialization parameters               cookie               Cookie value##             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.


pageContext object

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

${pageContext.request.queryString}

Scope object

pageScope, requestScope, sessionScope, and applicationScope variables are used to access variables stored at each scope level.

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

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

For example, to access a parameter named order, you can use the expression: ${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";
%>
<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.


header and headerValues ​​objects

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

For example, to access an information 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 information 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:

jsp-expression-language.jpg

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

Implicit objectDescription
              pageContext