JSP | Expression tag

Expression tag is one of the scripting elements in JSP.

Expression Tag in JSP is used for writing your content on client side. We can use this tag for displaying information on client browser. The JSP Expression tag transforms the code into an expression statement that
converts into a value in the form of string object and insert into the implicit output object.

Syntax of JSP tag

filter_none

edit
close

play_arrow

link
brightness_4
code

<%= expression %>

chevron_right


Difference between Scriptlet Tag and Expression Tag

  1. In Scriptlet tag, it’s Evaluates a Java expression. Does not display any result in the HTML produced. Variables declared have only local scope, so cannot be accessed from elsewhere in the .jsp . but in Expression Tag it’s Evaluates a Java expression. Inserts the result (as a string) into the HTML in the .jsp
  2. We dont need to write out.println in Expression tag for printing anything because these are
    converted into out.print() statement and insert into the _jspService(-, -) of the servlet class by the container.
Example



filter_none

edit
close

play_arrow

link
brightness_4
code

<html>  
<body>  
<%= GeeksforGeeks %>  <!-- Expression tag -->
</body>  
</html>

chevron_right


Output

Using expression tag

filter_none

edit
close

play_arrow

link
brightness_4
code

<%@ page language="java" contentType="text/html; 
charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<html>
  
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>GeeksforGeeks</title>
</head>
  
<body>
<% out.println("Hello Geeks "); %> <!-- Sriptlet Tag-->
<% int n1=10; int n2=30; %><!-- Sriptlet Tag-->
<% out.println("<br>sum of n1 and n2 is "); %> <!-- Sriptlet Tag-->
<%= n1+n2 %> <!-- Expression tag -->
</body>
  
</html>

chevron_right


Output



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.