BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

JSP Expression Tag

Last Updated: July 27, 2022 by Chaitanya Singh | Filed Under: JSP tutorial

JSP Expression tag evaluates the expression placed in it, converts the result into String and send the result back to the client through response object. Simply put, it writes the result to the client(browser).

Syntax of expression tag in JSP:

<%= expression %>

JSP Expression tag Example 1: Expression of values

Here we are simply passing the expression of values inside expression tag. The numbers along with mathematical operators such as +, -, * is placed inside an expression tag. The calculation of this expression is done and the result is displayed.

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

Output: 
JSP Expression Tag

JSP Expression tag Example 2: Expression of variables

In this example we have initialized three variables and passed the expression of variables in the expression tag for result evaluation.

<html>
<head>
   <title>JSP expression tag example2</title>
</head>
<body>
  <%
  int a=10;
  int b=20;
  int c=30;
  %>
  <%= a+b+c %>
</body>
</html>

Output:
JSP Expression Tag

Expression tag Example 3: String and implicit object output

In this example we have set an attribute using application implicit object and then displaying the value of that attribute and a simple string on another JSP page using expression tag.
index.jsp

<html>
<head>
<title> JSP expression tag example3 </title>
</head>
<body>
  <% application.setAttribute("MyName", "Chaitanya"); %>
  <a href="display.jsp">Click here for display</a>
</body>
</html>

display.jsp

<html>
<head>
<title>Display Page</title>
</head>
<body>
 <%="This is a String" %><br>
 <%= application.getAttribute("MyName") %>
</body>
</html>

Output:
JSP Expression Tag
JSP Expression Tag

Expression tag Example 4: using method inside tag to display current date/time

The getTime() method of Calendar class returns date and time together in a form like this: Mon Feb 24 16:45:47 UTC 2022. We are using this method inside expression tag to display current date and time. You can also refer this guide to find current date time in java.

<html>
<body>
Current Date and Time: <%= java.util.Calendar.getInstance().getTime() %>
</body>
</html>
❮ JSP Scriptlet TagJSP declaration tag ❯

Top Related Articles:

  1. JSP Scriptlet Tag (Scripting elements)
  2. Include Directive in JSP
  3. Exception handling in JSP
  4. Java Server Pages (JSP) Life Cycle
  5. JSP forward action tag

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

JSP Tutorial

  • Basics of JSP
  • Life cycle of JSP
  • JSP in Eclipse IDE

Scripting Elements

  • Scriptlet Tag
  • Expression tag
  • Declaration tag

Implicit Objects

  • Implicit Objects
  • JSP Request
  • JSP Response
  • JSP Config
  • JSP Application
  • JSP Session
  • JSP Out
  • JSP pageContext
  • JSP Exception
  • Validate session

JSP directives

  • JSP Directives
  • Include Directive

JSP Exception

  • Exception handling

JSP Action

  • Action tags
  • Include action
  • Forward action
  • useBean, setProperty & getProperty

Expression language

  • Expression language

JSP Custom Tags

  • Custom Tags
  • Custom tag example
  • JSP Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap