Overview of JSP Syntax Element
 
Object Scopes
 
Objects in a JSP page, whether explicit or implicit, are accessible within a particular scope.
In the case of explicit objects, such as a JavaBean instance created in a jsp:useBean action, we can explicitly set the scope with the following syntax, as in the example :
 
scope="scopevalue"
 
There are four possible scopes:
1. scope="page" (default scope):
The object is accessible only from within the JSP page where it was created. A page-scope object is stored in the implicit pageContext object. The page scope ends when the page stops executing.
Note that when the user refreshes the page while executing a JSP page, new instances will be created of all page-scope objects.
 
2. scope="request":
The object is accessible from any JSP page servicing the same HTTP request that is serviced by the JSP page that created the object. A request-scope object is stored in the implicit request object. The request scope ends at the conclusion of the HTTP request.
 
3. scope="session":
The object is accessible from any JSP page that is sharing the same HTTP session as the JSP page that created the object. A session-scope object is stored in the implicit session object. The session scope ends when the HTTP session times out or is invalidated.
 
4. scope="application":
The object is accessible from any JSP page that is used in the same Web application as the JSP page that created the object, within any single Java virtual machine. The concept is similar to that of a Java static variable. An application-scope object is stored in the implicit application servlet context object. The application scope ends when the application itself terminates, or when the JSP container or servlet container shuts down.
 
We can think of these four scopes as being in the following progression, from narrowest scope to broadest scope:
page < request < session < application
 
If we want to share an object between different pages in an application, such as when forwarding execution from one page to another, or including content from one page in another, we cannot use page scope for the shared object; in this case, there would be a separate object instance associated with each page. The narrowest scope we can use to share an object between pages is request.