Using JSP Declaration XML Tag

The following example illustrates using <jsp:declaration> tag. This is an alternative to the <%! %> (declaration).
     This tag is used to declare global variables or write member methods of the jasper-generated servlet class.

index.jsp

<jsp:declaration>
    int x=10;
    String st;
    public void print()
    {
    st="hello";
    }
</jsp:declaration>
<jsp:scriptlet>
    out.println(x);
    // initialize st
    print();
    out.println(st);
</jsp:scriptlet>

Note: You can write multiple <jsp:declaration>, <jsp:scriptlet> tags.

Previous: <jsp:scriptlet> tag
Next: <jsp:include> tag


Posted by at on

Tags: Java Server Pages,