Name

Declaration

Synopsis

Declarations are used to declare a scripting language variable or method. The content must be a complete valid declaration in the scripting language defined by the page directive. The JSP implicit variables aren’t visible in a declaration element.

When the scripting language is Java, a variable declared by a declaration element ends up as an instance variable in the JSP page implementation class. It’s therefore visible to parallel threads (requests) processing the page and needs to be handled in a thread-safe manner. A thread-safe alternative is to declare variables within a scriptlet element instead. It then becomes a local variable of the method in the page implementation class used to process each request and isn’t shared by parallel threads.

Syntax 1: In a regular JSP page

<%! declaration %>

Syntax 2: In a JSP Document (a JSP page written in XML syntax)

<jsp.declaration>declaration</jsp:declaration>

Attributes

None

Example

<%! int globalCounter = 0; %>

Get JavaServer Pages, Second Edition 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.