JSP.2.8.2 Objects and Scopes
A JSP page can create and/or access some Java objects when processing a
request. The JSP specification indicates that some objects are created implicitly,
perhaps as a result of a directive (see Section JSP.2.8.3, “Implicit Objects). Other
objects are created explicitly through actions, or created directly using scripting
code. Created objects have a scope attribute defining where there is a reference to
the object and when that reference is removed.
The created objects may also be visible directly to scripting elements through
scripting-level variables (see Section JSP.2.8.3, “Implicit Objects).
Each action and declaration defines, as part of its semantics, what objects it
creates, with what scope attribute, and whether they are available to the scripting
elements.
Objects are created within a JSP page instance that is responding to a request
object. There are several scopes:
• page - Objects with page scope are accessible only within the page where they
are created. All references to such an object shall be released after the response
is sent back to the client from the JSP page or the request is forwarded somewhere
else. References to objects with page scope are stored in the pageContext
object.
• request - Objects with request scope are accessible from pages processing the
same request where they were created. References to the object shall be released
after the request is processed. In particular, if the request is forwarded
to a resource in the same runtime, the object is still reachable. References to
objects with request scope are stored in the request object.
• session - Objects with session scope are accessible from pages processing requests
that are in the same session as the one in which they were created. It is
not legal to define an object with session scope from within a page that is not
session-aware (see Section JSP.2.10.1, “The page Directive). All references to
the object shall be released after the associated session ends. References to
objects with session scope are stored in the session object associated with the
page activation.
• application - Objects with application scope are accessible from pages processing
requests that are in the same application as they one in which they were
created. Objects with application scope can be defined (and reached) from pages
that are not session-aware. References to objects with application scope are
stored in the application object associated with a page activation. The
application object is the servlet context obtained from the servlet configuration
object. All references to the object shall be released when the runtime environment
reclaims the ServletContext.
A name should refer to a unique object at all points in the execution, that is all
the different scopes really should behave as a single name space. A JSP container
implementation may or may not enforce this rule explicitly due to performance
reasons.