Expression Language in JSP

In the previous article, we have seen all the Action Tags that are available on the JSP page. In this article, we will see the Expression Language (EL) which is a newly added feature in JSP technology version 2.0.

We will cover What is Expression Language with a simple example, Implicit objects in EL, the precedence of operators in EL, and Reserve words in EL in this article.

What is Expression Language(EL)?

The Expression language is a new feature introduced in JSP technology version 2.0. It allows the page to use simple expressions to dynamically read data from the JAVA Bean component.

  • Expression language provides the operators for doing logical and arithmetic operations.
  • Inside a JSP EL expression, we can use integers, floating-point numbers, strings, loops, etc.
  • In order to get the attribute values, JSP EL uses Dot(.) operator.
  • EL is always within curly bracket prefixed with $ sign for example ${}.
  •  If we want to disable EL expression we can set isELIgnored to TRUE.
  • If any attribute is not found JSP EL returns null.

Syntax

The expression here specifies what expression we will use. Whenever the compiler encounters the ${} in an attribute, It generates code to evaluate it.

${ expression }

To get a better idea, of how expression language works let’s see the below example. In this example, we are multiplying two numbers using EL.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body bgcolor="#A5D8F3">
<h2>By using Expression Langauge</h2>
${25*25}
</body>
</html>

Output

Implicit Objects in Expression Language(EL)

There are many implicit Objects available in the Expression Language. They are as follows.

Let’s see some of them in detail

EL param

This EL is used to map the parameters to a single value. Through this object, we get access to the parameter values as we do normally in servlet like request.getParameter() method.

Example

In this example, we will access the parameter using the param EL.

page1.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body bgcolor="#A5D8F3">
<form action="page2.jsp">
Enter Name: <input type="text" name="name"><br>
<input type="submit" value="SUBMIT">
</form>
</body>
</html>

page2.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title></title>
</head>
<body>
Have a nice day, ${param.name}!!!
</body>
</html>

Output

EL headers and header values Objects

It maps the header to a single or an array of values. It gives you access to the header values normally we do with request.getHeader() method. Following is the way we can access a header name Host.

${header.abc} or ${header["abc"]}.

Example

In this example, we will use the header EL to fetch the Host of the client.

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%String title="Host Example"; %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body bgcolor="#A5D8F3">
<h1><%out.println(title); %></h1>
<h3>${header.Host}</h3>
</body>
</html>

Output

Precedence of Operators

There are many operators available that have been provided in Expression Language. The below table shows from highest to lowest.

Reserve Words in EL

There are some reserved words in EL which we don’t use as an identifier in JSP.

Thus, Expression Language makes it easy to access the application for the data stored in Java Bean also helps to use the expression in JSP Page.

In the next article of this tutorial, we will discuss Exception handling on the JSP page. We will also see a simple example for Handling runtime Exception in the JSP page.