Objects in a JSP page, including user-created objects (for example, JavaBean objects) and JSP implicit objects, have a scope attribute. The scope defines within what time and in which JSP page these objects can be accessed. For example, the session object can be accessed on multiple pages during the session. The application object can be accessed throughout the life cycle of the web application. In JSP, there are 4 types of scopes as shown below.
·page range
Objects with page scope are bound to the javax.servlet.jsp.PageContext object. Objects within this scope can only be accessed on the page where the object was created. You can call the getAttribute() method of the implicit object pageContext to access objects with this scope type (the pageContext object also provides getAttribute methods for accessing other scope objects). The pageContext object itself also belongs to the page scope. When the _jspService() method of the Servlet class is executed, the reference to the object belonging to the page scope will be discarded. Objects in the page scope are created every time the client requests a JSP page, and are deleted after the page sends a response back to the client or the request is forwarded to other resources.
·Request scope
Objects with request scope are bound to the javax.servlet.ServletRequest object. You can call the getAttribute() method of the request implicit object to access objects with this scope type. Objects in this range can be accessed in the page redirected by calling the forward() method or the page included by calling the include() method. It should be noted that because the request object is different for each client request, objects in this scope must be recreated and deleted for each new request.
·session scope
Objects with session scope are bound to the javax.servlet.http.HttpSession object. You can call the getAttribute() method of the session implicit object to access objects with this scope type. The JSP container creates an HttpSession object for each session. During the session, objects within the session scope can be accessed.
·application scope
Objects with application scope are bound to javax.servlet.ServletContext. You can call the getAttribute() method of the application implicit object to access objects with this scope type. While the web application is running, all pages can access objects in this scope.