Expression tags are one of the most useful scripting elements of JSP. Expression tags use special tags to print the different java expressions and output directly on the client-side. You can also use this for displaying information on your client browser. In this chapter, you will learn about implementing expression tags and know how to use it.



What is Expression Tag?

The expression tag is used to evaluate Java's expression within the JSP, which then converts the result into a string and forwards the result back to the client browser via the response object. Essentially, it is implemented for printing the result to the client (browser). An expression tag can hold any java language expression that can be employed as an argument to the out.print() method.

The syntax of the expression tag in JSP looks something like:

Syntax:

<%= expression %>

This is how the JSP container sees this when it encounters the above expression:

<%= (6*4) %>

It turns out to be:

out.print((6*4));

It is to be noted that you will never terminate an expression using semicolon within Expression Tag. Like this:

<%= (6*4); %>

Here is an example of an Expression tag:

Example:

<!DOCTYPE html>
<html>
    <head>
        <title>Expression Tag in JSP</title>
    </head>
    <% int rollNo = 02; %>
    <body>
        The next roll number is: <%= ++rollNo %>
    </body>
</html>

Different Expression Tags with Different Expression Types:

Expression of Values

In this chase, you have to pass the expression of values within the expression tag.

Example:

<!DOCTYPE html>
<html>
    <head>
        <title>JSP expression tag example1</title>
    </head>
    <body>
        <%= 2+4*5 %>
    </body>
</html>

Expression of Variables

In this case, you have to initialize a few variables, which can then be passed as the expression of variables within the expression tag in order to evaluate the result.

Example:

<!DOCTYPE html>
<html>
    <head>
        <title>JSP expression tag example2</title>
    </head>
    <body>
        <% int g = 60; int k = 40; int r = 20; %>
        <%= g+k+r %>
    </body>
</html>

String with Implicit Object Output

Here, you have to set an attribute using an application implicit object and then display it with a simple string on another JSP page via an expression tag.

Example(start.jsp):
<!DOCTYPE html>
<html>
    <head>
        <title>Example of JSP expression tag</title>
    </head>
    <body>
        <% application.setAttribute("EmpName", "Alex"); %>
        <a href="show.jsp"> Click to See </a>
    </body>
</html>
Example(show.jsp):
<!DOCTYPE html>
<html>
    <head>
        <title>Show Employee Name Page</title>
    </head>
    <body>
        <%="A String value:" %> <br />
        <%= application.getAttribute("EmpName") %>
    </body>
</html>

Difference Between Scriptlet Tag and Expression Tag

  1. In Scriptlet tag, it evaluates your Java expression but does not print or show your result in conjunction with the HTML created. The declared variables have a local scope only and hence can't take access from another place in the .jsp. In contrast, the Expression Tag has the capability to evaluate the Java expression. It is used for inserting the result in the form of a string in conjunction with the HTML in the JSP.
  2. You don't have to write the out.println in the expression tag to print the expression based output because these are changed into out.print() statement (as shown above the process of transformation of expression), which gets inserted by the container in the _jspService(-, -) of the servlet class.


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram