declaration Tag

As you saw earlier, the Java code embedded in the JSP along with the HTML code is parsed and combined into the service() method of the servlet generated as a result of parsing the JSP. What if you needed to define variables or methods that need to be put outside the service() method? The declaration tag should be used in such cases.

Structure of the declaration Tag

The JSP declaration tag can be written in two ways:

<%! 
// java variables instantiated here
%>
or
<jsp:declaration>
// java code
</jsp:declaration>

The declaration tag is one of the simplest tags available in JSP:

<%! 
int myGlobalVar = 0;
ClassA myclassA - new ClassA();
%>

or

 <jsp:declaration> int myGlobalVar = 0; ClassA myclassA - new ClassA(); </jsp:declaration> ...

Get Sams Teach Yourself BEA WebLogic Server 7.0™ in 21 Days now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.