BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

JSP Declaration tag

Last Updated: July 27, 2022 by Chaitanya Singh | Filed Under: JSP tutorial

JSP Declaration tag is a block of java code used for declaring class wide variables and methods. The code placed inside declaration tag gets initialized during JSP initialization phase and has class scope. JSP container keeps this code outside of the service method _jspService() to make them class level variables and methods.

As we know that variables can be initialized using scriptlet too but those declaration being placed inside _jspService() method which doesn’t make them class wide declarations. On the other side, declaration tag can be used for defining class level variables, methods and classes.

Syntax of declaration tag:

<%!  Declaration %>

Difference between JSP Scriptlet tag and Declaration tag

JSP Scriptlet Tag JSP Declaration Tag
The JSP scriptlet tag can only be used to declare variables, it cannot declare methods. The JSP declaration tag can be used to declare variables as well as methods.
The declaration of scriptlet tag is placed inside the _jspService() method. The variables declared inside _jspService() are local so these variables are not available class wide. The declaration of jsp declaration tag is placed outside the _jspService() method. The variables and methods declared by JSP declaration tag are available at class level.

Example 1: Variables declaration using JSP Declaration tag

In this example we have declared two variables inside declaration tag and displayed them on result page using expression tag. The variables declared are name and age, where name is a string type variable and age is an integer variable. We accessed the value of these variables on the JSP page using expression tag.

<html> 
<head>
 <title>Declaration tag Example1</title>
</head>
<body> 
<%! String name="Chaitanya"; %> 
<%! int age=27; %> 
<%= "Name is: "+ name %><br>
<%= "AGE: "+ age %> 
</body> 
</html>

Output:
JSP Declaration tag - variables

Example 2: Methods declaration using JSP Declaration tag

In this example we have declared a method sum using JSP declaration tag. This method accepts three integer parameters and returns the sum of these numbers. Here, we are passing three numbers 10, 40 and 50 to the method sum() declared using JSP declaration tag. The method returns the sum of these numbers which is 100 as displayed on the output screenshot.

<html> 
<head>
 <title>Methods Declaration</title>
</head>
<body> 
  <%! 
  int sum(int num1, int num2, int num3){ 
  return num1+num2+num3; 
  } 
  %> 

  <%= "Result is: " + sum(10,40,50) %> 
</body> 
</html>

Output:
Sum of all three integers gets displayed on the browser.
JSP Declaration tag - methods

❮ JSP expression tagJSP Implicit Objects ❯

Top Related Articles:

  1. JSP Directives – Page, Include and TagLib
  2. JSP Scriptlet Tag (Scripting elements)
  3. Exception handling in JSP
  4. Include Directive in JSP
  5. Java Server Pages (JSP) Life Cycle

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. mahesh agga says

    September 19, 2015 at 6:03 PM

    Hiii Sir your tutorials are very easy to understand… and please upload the webservices tutorials as sooon as possible

    Reply
  2. manjunath k says

    December 17, 2016 at 7:18 AM

    hep me related to structure concepts in jsp…….

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

JSP Tutorial

  • Basics of JSP
  • Life cycle of JSP
  • JSP in Eclipse IDE

Scripting Elements

  • Scriptlet Tag
  • Expression tag
  • Declaration tag

Implicit Objects

  • Implicit Objects
  • JSP Request
  • JSP Response
  • JSP Config
  • JSP Application
  • JSP Session
  • JSP Out
  • JSP pageContext
  • JSP Exception
  • Validate session

JSP directives

  • JSP Directives
  • Include Directive

JSP Exception

  • Exception handling

JSP Action

  • Action tags
  • Include action
  • Forward action
  • useBean, setProperty & getProperty

Expression language

  • Expression language

JSP Custom Tags

  • Custom Tags
  • Custom tag example
  • JSP Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap