JSP Expression Element


JSP Expression Tag :

  1. In JSP expression tag, Expression is evaluated and evaluated expression will be converted to a String.
  2. Converted expression is inserted where the expression appears in the JSP file.
  3. As an expression is converted to a String we can use it within JSP page anywhere inside tag
  4. Any valid expression according to the Java Language Specification can be written inside JSP Expression Tag
  5. Semicolon at the end an expression is not allowed

Syntax of JSP Expression Tag :

<%= expression %>

XML equivalent of the above syntax as follows :

<jsp:expression>
   code fragment
</jsp:expression>

Simple Snippet :

We can embed Scriplets inside the HTML tags to make them stylish.

<%@ page language="java" 
         contentType="text/html; charset=ISO-8859-1"
	 pageEncoding="ISO-8859-1" %>
<html>
<head>
<meta http-equiv="Content-Type" 
      content="text/html; charset=ISO-8859-1">
<title>Expression Tag</title>
</head>
<body>
     <%=40 + 49%>
</body>
</html>

Output :

89

Consider the following example to print the current date -

<%@ page language="java" 
         contentType="text/html; charset=ISO-8859-1"
	 pageEncoding="ISO-8859-1" %>
<html>
<head>
<meta http-equiv="Content-Type" 
      content="text/html; charset=ISO-8859-1">
<title>Expression Tag</title>
</head>
<body>
  Today's date: <%= (new java.util.Date()).toLocaleString()%>
</body>
</html>

Output :

Today's date: Apr 3, 2025 1:32:25 AM

Note :

Using semicolon at the end of expression element is not allowed -
JSP Error - Semicolon after expression