Scripting Elements
Scripting
elements let you add small pieces of
code to a JSP page, such as an if statement that
generates different HTML depending on some condition. The scripting
code must be written in the language defined by the
page directive. It is executed when the JSP page
is requested.
Declaration
A
declaration starts with
<%!
and ends with
%>. The content between the start and the end
characters must be a complete, valid declaration in the scripting
language defined by the page directive. The JSP
implicit variables are not visible in a declaration element.
A declaration can be used to declare a scripting language variable or method. 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 is 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 is not shared by parallel threads.
Example:
<%! int globalCounter = 0; %>
Expression
An expression starts with
<%=
and
ends with %>. The content between the start and
the end characters must be a complete, valid expression in the
scripting language defined by the page directive that results in or can be converted to a string. All JSP implicit variables are visible in an expression ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access