Table of contents
1.
Introduction
2.
What is an Expression tag
3.
Different Expression Tags with Different Expression Types
3.1.
Expression of Values
3.2.
Expression of Variables
4.
String with Implicit Object Output 
5.
Basic Operations in Expression Language
6.
Functions in JSP Expression Language 
7.
JSP Expression Language Implicit Objects
8.
Scope Objects
9.
FAQs
10.
Key Takeaways
Last Updated: Mar 27, 2024

JSP Expression Language

Author Gunjan Ahuja
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The expression tags are one of the most efficient scripting elements of JSP. These are the unique tags used to print the different java expressions and result directly on the client-side. We can also use this for portraying information on our client browser. We will understand how to implement expression tags and use them through this blog.

What is an Expression tag

We can use expression tags to analyze Java's expression within the JSP, which converts the output into a string and moves the result back to the client browser with the reference of the response object. It is implemented for fetching the result to the client (browser). An expression tag can have any java language expression that can be used as an argument to the out.print() method.

Syntax

The following is an example of the JSP expression tag, which shows how it will look.

<%= declaration %>
You can also try this code with Online Java Compiler
Run Code

Note: We must not end the statement with a semicolon in case of an expression tag.
Let's see how JSP expression will see it

<%= (7+5) %>
You can also try this code with Online Java Compiler
Run Code

It will be converted to

out.print((7+5));
You can also try this code with Online Java Compiler
Run Code

Example

<html>  
<body>  
<%= "JSP Expression Language" %>  
</body>  
</html>
You can also try this code with Online Java Compiler
Run Code

Output

Let's take another example to print the registration number of the next student in order.

Code

<!DOCTYPE html>
<html>
    <head>
        <title>Expression Tag</title>
    </head>
    <% int RegNo = 1002; %>
    <body>
        The next registration number is: <%= ++RegNo %>
    </body>
</html>
You can also try this code with Online Java Compiler
Run Code

Output

Different Expression Tags with Different Expression Types

Let's look at the different expression tags with different expression types.

Expression of Values

Let's see how to pass the expression of values within the expression tag.

Example

<!DOCTYPE html>
<html>
    <head>
        <title>expression of values in expression tag</title>
    </head>
    <body>
        <%= 8*3 %>
    </body>
</html>
You can also try this code with Online Java Compiler
Run Code

Output

Expression of Variables

To evaluate the result, we need to initialize some of the variables to be passed as the expression of variables within the expression tag.

Example

<!DOCTYPE html>
<html>
    <head>
        <title>Expression of variables</title>
    </head>
    <body>
        <% int a = 8; int b = 7; int c = 6; %>
        <%= a+b*c %>
    </body>
</html>
You can also try this code with Online Java Compiler
Run Code

Output

String with Implicit Object Output 

Here, we can set an attribute using an implicit application object and then show it with a simple string on another JSP page with the help of an expression tag.

example.jsp

<!DOCTYPE html>
<html>
    <head>
        <title>Example of JSP expression tag</title>
    </head>
    <body>
        <% application.setAttribute("StudentName", "Shiv"); %>
        <a href="display.jsp"> Click and See </a>
    </body>
</html>
You can also try this code with Online Java Compiler
Run Code

display.jsp

<!DOCTYPE html>
<html>
    <head>
        <title>Show Student Name Page</title>
    </head>
    <body>
        <%="Name:" %> <br />
        <%= application.getAttribute("StudentName") %>
    </body>
</html>
You can also try this code with Online Java Compiler
Run Code

Output

On clicking, the following is obtained

Basic Operations in Expression Language

The JSP Expression Language(EL) supports Java's arithmetic and logical operators. Let's go through some of the basic operators.

OPERATOR

DESCRIPTION

[]

To access an array or List element.

.

To access a map entry.

( )

To group subexpression to change the evaluation order.

+

Addition

 

-

Subtraction

*

Multiplication

/

Division

%

Modulus or remainder

!=

To check the inequality.

<

To check lesser than the value.

>

To check greater than the value

>=

To check greater than or equal to

<=

To check lesser than or equal to

|| or OR

To check for logical OR

&& or AND

To check for logical AND

! or not

Unary Boolean Complement

empty

To check empty variable values

Functions in JSP Expression Language 

JSP Expression language allows us to use functions in expressions. These functions must be defined in the custom tag libraries. Following is the syntax:

${ns:fun(value1, value2, ...)}
You can also try this code with Online Java Compiler
Run Code

Here ns is the namespace of the function, fun is the function's name, and value1 is the first parameter value. For instance, the function fn: length is part of the Java Standard Tag Library. To fetch the length of the string following function can be used:

${fn:length("Fetch the length")}
You can also try this code with Online Java Compiler
Run Code

JSP Expression Language Implicit Objects

It supports the following implicit objects.

IMPLICIT OBJECT

DESCRIPTION

pageScope Scoped variables from the page scope
sessionScope Scoped variables from session scope
requestScope Scoped variables from request scope
application scope Scoped variables from the application scope
param Gets parameters as strings
param values Gets parameters as a collection of strings
header Request HTTP headers as strings
cookie Cookie values
initParam Gets initialized parameters
pageContext The JSP PageContext object for the current page

Scope Objects

The page scope, requestScope, sessionScope, and application scope variables give access to variables stored at each scope level.

FAQs

  1. How to disable EL expression in JSP?
    It can be done by setting the JSP page directive isELIgnored attribute value to TRUE.
     
  2. Why use expression language?
    It simplifies the accessibility of data stored in the Java Bean component and other objects like request, session, application, etc.
     
  3. What is the difference between a scriptlet tag and an expression tag?
    The scriptlet tag evaluates our Java expression but does not print the result with the HTML created. The declared variables have a local scope only and hence can't take access from another place in the .jsp Whereas, the Expression Tag can evaluate the Java expression. It inserts the result in the form of a string together with the HTML in the JSP.

Key Takeaways

This blog was all about JSP Expression Language. All the syntaxes and the basic knowledge that is being shared through this blog will help you crack the interview questions and write the code effectively. 

Check out other blogs like Introduction to JSPJSP Filters, etc. We hope you found this blog on JSP Expression Language useful. Liked the blog? Then feel free to upvote and share it.

Live masterclass