< Zurück | Inhalt | Weiter >

19.3.2 Declaration

The other place that code can be placed is outside the doGet() and doPost(). It is still inside the class definition for the servlet class that gets generated from the JSP, but it is not inside any method. Such code is delimited like this:

<%! code %>

The exclamation mark makes all the difference. Since it’s outside any method, such code typically includes things like variable declarations and complete method declarations. For example:

<%! public static MyType varbl; public long



long retval = 0L;

retval *= varbl.toLong(); return retval;



If you tried to do something like this inside of a scriptlet, you would get errors when the server tries to compile your JSP. Such syntax belongs at the outer lexical level. The use of the <%! ... %> syntax puts it there.