beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
    • Learn jQuery
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

JSP Declaration tag – JSP Tutorial

By Chaitanya Singh | Filed Under: JSP tutorial

Declaration tag is a block of java code for declaring class wide variables, methods and classes. Whatever placed inside these tags 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 %>

Example 1: Variables declaration

In this example we have declared two variables inside declaration tag and displayed them on client 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:

declaration-tag-example1

Example 2: Methods declaration

In this example we have declared a method sum using JSP declaration tag.

<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.

methods-declaration

Let us know if you have any questions.

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
  • Declaration tag
  • Expression tag
  • Scriptlets
  • Directives
  • Include Directive
  • Param Directive
  • Exception handling
  • Action tags
  • Include action
  • Forward action
  • useBean, setProperty & getProperty
  • Implicit Objects
  • Session implicit object
  • Validate session
  • Request implicit object
  • Response implicit object
  • Out implicit object
  • Application implicit object
  • Config implicit object
  • pageContext implicit object
  • Exception implicit object
  • Expression language
  • Custom Tags
  • Custom tag example
  • JSP Interview Q

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap