JSP Tutorials

JSP declaration tag
Previous Home Next
adplus-dvertising

JSP Declaration Tag

In the JSP declaration tag is used to declare fields and methods.

This code written inside the jsp declaration tag is placed outside the service() method of auto generated servlet.

Syntax

    <%!  field or method declaration %>  

    <%!  field or method declaration %>  

Difference between JSP Scriptlet tag and Declaration tag

Jsp Scriptlet TagJsp Declaration Tag
In this tag can only declare variables not methods. And this tag can declare variables as well as methods.
In this declaration of scriptlet tag is placed inside the _jspService() method. And in this declaration of jsp declaration tag is placed outside the _jspService() method.

JSP declaration tag(declares field)

In this example of JSP declaration tag using the jsp expression tag and declaring the field.

    <html>  
    <body>  
    <%!   
    int cube(int n){  
    return n*n*n*;  
    }  
    %>  
    <%= "Cube of 3 is:"+cube(3) %>  
    </body>  
    </html>  

    <html>  
    <body>  
    <%!   
    int cube(int n){  
    return n*n*n*;  
    }  
    %>  
    <%= "Cube of 3 is:"+cube(3) %>  
    </body>  
    </html>  
Previous Home Next