Four scopes: PageContext, request, session,application
What is the scope, first let's look at the effect:
The process is like this, when we visit 04-01/index.jsp, respectively, to PageContext, request, session,
Application The variables in four scopes to accumulate. (Of course, the variable is not present, if the variable does not exist, then
Initialize the variable to 1. After the calculation is complete, jump from index.jsp execution forward to test.jsp. one more time in the test.jsp.
Accumulate and then show these four integers.
Judging from the results shown, we can intuitively draw the conclusion that:
The variables in page cannot be passed from index.jsp to test.jsp. As soon as the page jumps, they are gone.
The variables in the request can span two pages before and after forward. But as soon as you refresh the page, they recalculate.
the variables in the session and the application are always cumulative and start to see no difference, as long as you close the browser, restart the browser again to access
On this page,the variables in the session are recalculated.
The variables in the application keep accumulating until you restart tomcat, otherwise it will keep getting bigger.
The scope specifies the validity period of the variable.
If you put the variable in the PageContext, it means that its scope is page, its valid range is only in the current JSP page.
You can use this variable from the start of placing the variable in PageContext to the end of the JSP page.
If you put the variable in the request, it means that it is scoped to request , and its valid range is the current demand period.
The so-called request period, that is, from the HTTP request initiation, to the end of the server processing, return the entire process of response. In this process, it is possible to make
jump through multiple JSP pages in a forward way , and you can use this variable on these pages.
If you put the variable in the session, it means that it is scoped to the session , and its valid range is the current one.
The so-called current session refers to the process of opening the browser from the user to the user closing the browser. This process may contain multiple
Request response. In other words, as long as the user does not close the browser, the server has a way to know that these requests are initiated by a person, the entire process is
Referred to as a session, and the variables placed in the session can be used in all requests for the current conversation.
If you put the variable in the application, it means that its scope is application, and its effective range is the entire application.
The entire application is launched from the app to the end of the app. We did not say "boot from server to server shutdown" because a service
Can deploy multiple applications, of course, if you shut down the server, you will shut down all the applications above.
Application The variables in the scope, they survive the longest, and if not manually deleted, they can be used all the time.
Unlike the three above, thevariables in the application can be shared by all users. If user A's operation modifies the application
The variable in the user B is given a modified value when it is accessed. This will not happen in any other scope,page, request,
The session is completely isolated, and any modification will not affect the other person's data.
We get the value of the variable using public Object getattribute (String name), using the public void SetAttribute
(String name, Object value) saves the value of the variable to the corresponding scope. An example of PageContext is:
Page
Integer countpage = (integer) pagecontext.getattribute ("Countpage");
if (countpage = = null) {
Pagecontext.setattribute ("Countpage", 1);
} else {
Pagecontext.setattribute ("Countpage", Countpage + 1);
}
This first takes the integer named Countpage from the PageContext , because the java.lang.Object type is returned, so a strong
Transformation into the shaping we need. The variable obtained here will return NULL if it does not exist , by judging countpage = = NULL to distinguish
If no variable exists, set to 1 if it does not exist, accumulate if it exists, and then use the SetAttribute () method to modify the
The value of the variable into PageContext.
Replace the PageContext with request, session, application can manipulate the variables in the other three scopes.
Note: from CSDN blog
[Four scopes of the original]java web