Updated April 3, 2023
Introduction to JSP Declaration tag
JSP (Java Servlet Pages) declaration is a tag used to declare methods and variables (fields). Declaration tag subpart of the “JSP Scripting elements”. The code we are writing inside the tag is actually placed outside the service() method. This service() method is auto-generated from the Servlet interface. This service() method always create multiple objects for multiple requests. But JSP declaration objects creates a single object for multiple requests as it is outside the service() method.
How does JSP Declaration tag Work in JSP?
It is worked based on the value provided within <%! %>( declaration tag) tag. Basically inside this tag, we declare variables and methods.
Syntax:
<%! variables or methods declaration%>
Difference between Declaration tag and Scriptlet tag
For better understanding, you must understand the difference between declaration and scriptlet tag.
Declaration Tag | Scriptlet Tag |
Declared inside <%! %> tag. | Declared inside <% %> tag. |
It can declare variables as well as methods. | It can declare only variables, not methods. |
This tag placed outside the _jspService() method. | This tag placed inside the _jspService() method. |
It creates only one object for multiple requests. | It creates multiple objects for multiple requests. |
Examples
Below are the examples:
Example #1
Variables inside the declaration tag.
Code:
<html>
<title>
JSP Declaration Tag
</title>
<body>
<!--declaring JSP declaration tag-->
<%!
//declaring and initializing int variable
int number=214;
//declaring and initializing float variable
float salary=40000.50;
//declaring and initializing long variable
longbigNo=787878;
//declaring and initializing char variable
char alp='N';
//declaring and initializing String variable
String name="Amardeep";
%>
<!--declaring JSP expression tag-->
<!--Used to display output in JSP-->
<%= "My Class Number in Graduation: "+number+"<br>" %>
<%= "My Salary in VerinonTechologies: "+salary+"<br>" %>
<%= "My Big Number: "+bigNo+"<br>" %>
<%= "My Favourite Alphabet: "+alp+"<br>" %>
<%= "My Friend Name is : "+name +"<br>" %>
</body>
</html>
Output:
Example #2
Variable with declaration introduction content with CSS styles.
Code:
<html>
<title>
JSP Declaration Tag
</title>
<body>
<!--declaring JSP declaration tag-->
<%!
//declaring and initializing String variable
String name="Paramesh";
%>
<!--declaring JSP expression tag-->
<!--Used to display output in JSP-->
<%="<h1 style='color:blue; text-align:center;'>Introduction to JSP Declaration Tag</h1>"%>
<%= "<p style='color:red; font-size:25px; border:solid 2px green'>JSP (Java Servlet Pages) declaration is tag used to declare methods and variables (fields). Declaration tag sub part of the JSP Scripting elements. The code we are writing inside the JSP declaration tag actually placed outside the service() method. This service() method is auto generated from Servlet interface. This service() method always created multiple object for multiple requests. But JSP declaration objects creates single object for multiple requests as it is outside the service() method.</p>" %>
<%= "<h2 style='color:maroon;'>My Name is: "+name+", Working as Software Engineer</h2><br>" %>
</body>
</html>
Output:
Example #3
The method inside declaration tag and content.
Code:
<html>
<title>
JSP Declaration Tag
</title>
<body>
<!--declaring JSP declaration tag-->
<%!
//declaring method
int getSum(int x, int y, int z)
{
return x+y+z;
}
%>
<!--declaring JSP expression tag-->
<!--Used to display output in JSP-->
<%="<h1 style='color:brown; text-align:center;'>Introduction to JSP Declaration Tag</h1>"%>
<%= "<p style='color:navy; font-size:25px; border:double 5px yellow'>JSP (Java Servlet Pages) declaration is tag used to declare methods and variables (fields). Declaration tag sub part of the JSP Scripting elements. The code we are writing inside the JSP declaration tag actually placed outside the service() method. This service() method is auto generated from Servlet interface. This service() method always created multiple object for multiple requests. But JSP declaration objects creates single object for multiple requests as it is outside the service() method.</p>" %>
<%= "<h2 style='color:blue; text-align:center;'>Declaring Method inside declaration Tag Output</h2>"%>
<%= "<h2 style='color:maroon;'>Sum of the numbers is : "+getSum(10,20,30) +"</h2>" %>
</body>
</html>
Output:
Example #4
Method and variables inside declaration tag and content.
Code:
<html>
<title>
JSP Declaration Tag
</title>
<body>
<!--declaring JSP declaration tag-->
<%!
//declaring variables
int r,temp,sum=0;
String out=" ";
//declaring method
String getPalindromeOrNot(int n)
{
temp=n;
while(n>0)
{
r=n%10; //getting remainder
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
out="Number is Palindrome";
else
out="Number is not Palindrome";
return out;
}
%>
<!--declaring JSP expression tag-->
<!--Used to display output in JSP-->
<%="<h1 style='color:orange; text-align:center;'>Introduction to JSP Declaration Tag</h1>"%>
<%= "<p style='color:brown; font-size:25px; border:dotted 5px pink'>JSP (Java Servlet Pages) declaration is tag used to declare methods and variables (fields). Declaration tag sub part of the JSP Scripting elements. The code we are writing inside the JSP declaration tag actually placed outside the service() method. This service() method is auto generated from Servlet interface. This service() method always created multiple object for multiple requests. But JSP declaration objects creates single object for multiple requests as it is outside the service() method.</p>" %>
<%= "<h2 style='color:green; text-align:center;'>Declaring Method inside declaration Tag Output</h2>"%>
<%= "<h2 style='color:fuchsia;'>Provided : "+getPalindromeOrNot(12321) +"</h2>" %>
</body>
</html>
Output:
Recommended Article
This is a guide to JSP Declaration. Here we discuss the Introduction and working along with its examples and code implementation. You can also go through our other suggested articles to learn more –