Easy Tutorial
❮ Jsp Form Processing Jsp Hits Counter ❯

JSP Expression Language

The JSP Expression Language (EL) makes it very easy to access data stored in JavaBeans. JSP EL can be used to create both arithmetic and logical expressions. Within JSP EL expressions, you can use integers, floating-point numbers, strings, the constants true and false, and null.


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

${expr}

Where expr refers to the expression. The common operators in JSP EL 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 using expression language as follows:

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

When the JSP compiler sees the ${} format in an attribute, it generates code to evaluate this expression and produces a substitute for the value of the expression.

You can also use expression language in the template text of a tag. For example, the <jsp:text> tag simply inserts its body text into the JSP output:

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

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

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

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

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

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

This way, EL expressions 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:

Operator Description
. Access a Bean property or a map entry
[] Access an array or list element
( ) Organize a subexpression to change precedence
+ Addition
- Subtraction or negation
* Multiplication
/ or div Division
% or mod Modulus
== or eq Test for equality
!= or ne Test for inequality
< or lt Test for less than
> or gt Test for greater than
<= or le Test for less than or equal to
>= or ge Test for greater than or equal to
&& or and Logical AND
|| or or Logical OR
! or not Logical NOT
empty Test for empty 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 for using functions is as follows:

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

ns refers to the namespace, func is the function name, param1 is the first parameter, param2 is 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 like this:

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

To use any function from a tag library, you need to install these libraries on your server and include them in your JSP file using the <taglib> tag.


JSP EL Implicit Objects

JSP EL supports the following implicit objects:

Implicit Object Description
pageScope Page scope
requestScope Request scope
sessionScope Session scope
applicationScope Application scope
param Request parameter, string
paramValues Request parameter, string collection
header HTTP header, string
headerValues HTTP header, string collection
initParam Context Initialization Parameter
cookie Cookie Value
pageContext The pageContext of the current page

You can use these objects in expressions just like variables. Several examples will be provided 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, to access the query string passed in the request object, you can do it like this:

${pageContext.request.queryString}

Scope Objects

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

For instance, if you need to explicitly access the box variable at the applicationScope level, you can access it like this: applicationScope.box.


param and paramValues Objects

The param and paramValues objects are used to access parameter values, using the request.getParameter method and 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 header information, 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";
%>
<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 result of running this example would be:

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

❮ Jsp Form Processing Jsp Hits Counter ❯