In JSP page, including objects created by the user (for example, JavaBean objects) and JSPhas a range attribute. The scope defines the time at which the objects can be accessed in which JSP pages. For example, a session object can be accessed on multiple pages during a conversation. Application objects can be accessed throughout the life cycle of a Web application. In the JSP, there are 4 ranges, as shown below.
Page Range
Objects that have page ranges are bound to the Javax.servlet.jsp.PageContext object. Objects within this scope can only be accessed in the page where the object is created. You can call the GetAttribute () method PageContext this suppressed object to access objects with this range type (the PageContext object also provides a getattribute method to access other scope objects). The PageContext object itself also belongs to the page range. When the _jspservice () method of the Servlet class completes, a reference to the object belonging to the page range is discarded. A page-scoped object that is created each time the client requests a JSP page, is deleted when the page sends back a response to the client, or when the request is forwarded (forward) to another resource.
Request Range
Objects with the request scope are bound to the Javax.servlet.ServletRequest object and can invoke the GetAttribute () method of the implied object of request to access objects with this range type. You can access an object within this scope either by calling the forward () method or by calling the included page of the Include () method. Note that because the request object is different for each client request, the object in this scope is re-created and deleted for each new request.
Session Range
An object with a session scope is bound to a Javax.servlet.http.HttpSession object, and the GetAttribute () method of the implied object of the session can be invoked to access objects with this range type. The JSP container creates a HttpSession object for each session, during which you can access objects within the session scope.
Application Range
An object with a application range is bound to Javax.servlet.ServletContext, and the GetAttribute () method application This suppressed object can be invoked to access objects with this range type. During a Web application run, all pages can access objects within that scope.
Let's look at the application of these 4 range objects with a few simple examples.
1. Test Page Range
test1.jsp
<% Pagecontext.setattribute ("name", "Zhangsan"); Out.println ("test1.jsp:"); Out.println (Pagecontext.getattribute ("name")); Out.println (""); Pagecontext.include ("test2.jsp"); %>
|
test2.jsp
<% Out.println ("test2.jsp:"); Out.println (Pagecontext.getattribute ("name")); %> |
To access test1.jsp, you will see the following output:
Test1.jsp:zhangsan
Test2.jsp:null
Note the properties saved in the PageContext object have a page range and can only be accessed on the same page.
2. Test request Scope
Modify the test1.jsp and test2.jsp as shown below.
test1.jsp
<% Request.setattribute ("name", "Zhangsan"); Out.println ("test1.jsp:"); Out.println (Request.getattribute ("name")); Out.println (""); Pagecontext.include ("test2.jsp"); %> |
test2.jsp
<% Out.println ("test2.jsp:"); Out.println (Request.getattribute ("name")); %> |
To access test1.jsp, you will see the following output:
Test1.jsp:zhangsan
Test2.jsp:zhangsan
Description The attributes saved in the Request object have the request scope and can be accessed during the lifetime of the request object. Will
Pagecontext.include ("test2.jsp"); |
To annotate this sentence, first visit the test1.jsp, and then visit the test2.jsp, you can see the following output:
Test2.jsp:null
This is because the client has started a new request.
3. Test Session Scope
Modify the test1.jsp and test2.jsp as shown below.
test1.jsp
<% Session.setattribute ("name", "Zhangsan"); %> |
test2.jsp
<% Out.println ("test2.jsp:"); Out.println (Session.getattribute ("name")); %> |
You can see the following output by accessing the test1.jsp first and then accessing test2.jsp in the same browser window:
Test2.jsp:zhangsan
Note the properties that are saved in the Session object have a scope of sessions, during which you can access objects within that scope.
If we close the browser, reopen the browser window, and visit test2.jsp after we have accessed test1.jsp, we will see the following output:
Test2.jsp:null
This is because the client started a new session with the server.
4. Test Application Range
Modify the test1.jsp and test2.jsp as shown below.
test1.jsp
<% Application.setattribute ("name", "Zhangsan"); %> |
test2.jsp
<% Out.println ("test2.jsp:"); Out.println (Application.getattribute ("name")); %> |
You can see the following output by first accessing the test1.jsp, then closing the browser, then opening the browser window and accessing the test2.jsp:
Test2.jsp:zhangsan
Explains that the properties saved in the Application object have a application scope that can be accessed during the time the Web application is running.