JSP | Declaration Tag - GeeksforGeeks

15 Jun, 2021
Declaration tag is one of the scripting elements in JSP.This Tag is used for declare the variables. Along with this, Declaration Tag can also declare method and classes. Jsp initializer scans the code and find the declaration tag and initializes all the variables, methods, and classes. JSP container keeps this code outside of the service method (_JSPService()) to make them class level variables and methods. 
Syntax of JSP-Declaration Tag 
HTML
<%!  inside this tag we can initialiseour variables, methods and classes  %>
Example of JSP Declaration Tag which initialize a string
HTML
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>GeeksforGeeks</title></head> <body><!--declaration of username variable....  --><%! String username="Geeks"; %> <!--In expression tag a string is initialised as Geeks --><%="Hello : "+username %> <!-- Displaying expression using Expression Tag --></body></html>
Output: 
Example of JSP Declaration Tag which initializes a method 
HTML
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>GeeksforGeeks</title></head><body> <html>       <body>        <%!        int factorial(int n)        {        if (n == 0)            return 1;           return n*factorial(n-1);        }          %>         <%= "Factorial of 5 is:"+factorial(5) %>        </body>       </html></body></html>
Output 
Difference between the JSP Expression, Declarative, and Scriptlet tags 
Expression tag: This tag contains a scripting language expression that is converted to a String and inserted where the expression appears in the JSP file. Because the value of an expression is converted to a String, you can use an expression within text in a JSP file. You cannot use a semicolon to end an expression.
Declaration tag: This declares one or more variables or methods for use later in the JSP source file. It must contain at least one complete statement. You can declare any number of variables or methods within one declaration tag, but you have to separate them by semicolons. The declaration must be valid in the scripting language used in the JSP file.You can add method to the declaration part.
Scriptlet tag: You can declare variables in the script-let and can do any processing. All the Scriptlet go to the inside service() method of the convert servlet.
 
anikaseth98
Java-JSP
Articles
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Time Complexity and Space Complexity
Mutex vs Semaphore
Understanding "extern" keyword in C
Analysis of Algorithms | Set 2 (Worst, Average and Best Cases)
SQL | Views
How to write a Pseudo Code?
SQL | GROUP BY
Recursive Practice Problems with Solutions
Little and Big Endian Mystery