JSP – Declaration Tags

JSP Declaration Tag

  • Declaration tag is used to declare one or more variables or methods at class level.
  • Variables and methods declare in declaration tag are initialized at the time of JSP initialization.
  • These variables and methods are kept outside of the service method by web container to make them class level.

Syntax:

<%!  Variable or method Declaration %>

welcome.jsp:

<html>
            <head>
                        <title>Declaration tag example</title>
            </head>
 
            <body>
                        <%!
                                    int sum(int a, int b)
                                    {
                                                return a + b;
                                    }
                        %>
 
                        <%
                                    out.println(“Sum of 10 and 20 = ” + sum(10, 20));
                        %>
            </body>
</html>
Scroll to Top