Do you think IIT Guwahati certified course can help you in your career?
No
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
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
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
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
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
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
The page scope, requestScope, sessionScope, and application scope variables give access to variables stored at each scope level.
FAQs
How to disable EL expression in JSP? It can be done by setting the JSP page directive isELIgnored attribute value to TRUE.
Why use expression language? It simplifies the accessibility of data stored in the Java Bean component and other objects like request, session, application, etc.
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 JSP, JSP Filters, etc. We hope you found this blog on JSP Expression Language useful. Liked the blog? Then feel free to upvote and share it.