Expression tag evaluates the expression placed in it, converts the result into String and send the result back to the client through response object. Basically it writes the result to the client(browser).
Syntax of expression tag in JSP:
<%= expression %>
JSP expression tag Examples
Example 1: Expression of values
Here we are simply passing the expression of values inside expression tag.
<html> <head> <title>JSP expression tag example1</title> </head> <body> <%= 2+4*5 %> </body> </html>
Output:
Example 2: Expression of variables
In this example we have initialized few 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:
Example 3: String and implicit object output
In this example we are setting up an attribute using application implicit object and then displaying 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:
Leave a Reply