JSP Declaration Tag

tutorial and example
tutorial and example

JSP declaration tag is used to declare variables and methods in JSP page. Unlike scriptlet tag, JSP container placed the content of declaration tag outside the jspService() method. So, variables and methods declared in declaration tag are static or instance.

The syntax of declaration tag is as follows:

<%! variable and method declaration %>

The variable and method inside declaration tag is up to the class level. We can put any number of variables and methods inside declarative tag.

Example of declaring variables in JSP Declaration Tag

Along with declaring variable, this example also shows that declaration tag doesn’t get memory at each request.

index.jsp

Output:

Example of declaring methods in JSP Declaration Tag

In this example, a method is declared inside declaration tag and has been accessing from scriptlet tag.

Index.jsp

Output: