Latest web development tutorials

JSP Expression Language

JSP Expression Language (EL) so that access data stored in a JavaBean becomes very simple. JSP EL can either be used to create an arithmetic expression can also be used to create a logical expression. In JSP EL expressions can use integer, floating-point numbers, strings, constants true, false, there is null.


A simple syntax

Typically, when you need to specify a property value when the JSP tags, simply use the string to:

<jsp:setProperty name="box" property="perimeter" value="100"/>

JSP EL expression allows you to specify a property value. A simple expression syntax is as follows:

${expr}

Wherein, expr refers to the expression. In the JSP EL generic operator is "." And "[]." These two operators allow you to access a wide variety of embedded JSP JavaBean properties through the object.

For example, the above <jsp: setProperty> tag 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 to see the "$ {}" after the format, it generates code to evaluate the expression, and generates a substitute to replace the value of the expression in the property.

You can also use the expression language in the template text tab. For example, <jsp: text> tag simply be inserted into the body text JSP output:

<jsp:text>
<h1>Hello JSP!</h1>
</jsp:text>

Now, in: using the expression <jsp text> tag in the body, like this:

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

EL expressions in parentheses can be used to organize sub-expression. For example $ {(1 + 2) * 3 equals 9}, but $ {1 + (2 * 3)} = 7.

Want to disable evaluation of EL expressions, then you need to use the page directive isELIgnored attribute to true:

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

Thus, EL expressions are ignored. If set to false, the container will be calculated EL expressions.


EL underlying operator

EL expression support provided by most Java arithmetic and logical operators:

Operators description
. Bean access a property or a mapping entry
[] Access an array or list of elements
() Organize a sub-expression to change the priority
+ plus
- Save or negative
* Multiply
/ Or div except
% Or mod Modulo
== Or eq Test for equality
! = Or ne Test whether unequal
<Or lt Test whether less than
> Or gt The test is greater than
<= Or le Test whether less than or equal
> = Or ge Test whether greater than or equal
&& Or and Test logic and
|| Or or Test logic or
! Or not Test negated
empty Test whether null

The JSP EL function

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

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

ns refers to a namespace (namespace), func is the name of the function, param1 refers to the first argument, param2 refers to the second argument, and so on. For example, there is a function fn: length, defined in JSTL library can be like this to get the length of a string:

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

To use any of the tag library functions, you will need these libraries installed on the server, and then use the <taglib> tag contains the libraries in the JSP file.


JSP EL implicit objects

Supports JSP EL implicit objects are listed in the following table:

Hidden objects description
pageScope Scope page
requestScope request scope
sessionScope session scope
applicationScope application scope
param Request object parameter string
paramValues Parameter Request object, string collection
header HTTP header string
headerValues HTTP header, string collection
initParam Context initialization parameters
cookie Cookie value
pageContext Current pageContext page

You can use these objects in an expression, like using the same variable. Next will give a few examples to better understand this concept.


pageContext objects

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

${pageContext.request.queryString}

Scope Objects

pageScope, requestScope, sessionScope, applicationScope variable is used to access data stored in each variable scoping levels.

For example, if you need to explicitly access box applicationScope variable layer, it can be accessed: applicationScope.box.


param and paramValues ​​objects

param and paramValues ​​object is used to access parameter values ​​by using the methods and request.getParameterValues ​​request.getParameter method.

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

The following examples show how to access the username request parameter:

<%@ 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>

param object returns a single string, paramValues ​​object is returned a string array.


header objects and headerValues

header and headerValues ​​object is used to access the header information by using the methods and request.getHeaders request.getHeader method.

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

The following examples show how to access the 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>

Results are as follows:

jsp-expression-language

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