Scope
Let's take a look at the effect:
The general process is as follows: when we access 04-01/index. jsp, we perform pageContext, request, session,
The variables in the four scopes of application are accumulated. (Of course, first determine whether the variable exists. If the variable does not exist, you must
Initialize the variable to 1 .) After the calculation is complete, execute forward from index. jsp to test. jsp. Run the test. jsp command again.
Then, the four integers are displayed.
From the displayed results, we can intuitively draw a conclusion:
The variables in the page cannot be passed from index. jsp to test. jsp. As long as the page jumps, they will disappear.
The variables in the request can span the two pages before and after the forward. However, you only need to refresh the page, and they will recalculate it.
The variables in the session and application have been accumulating and there is no difference at first. You only need to close the browser and restart the browser to access
On this page, the variables in the session are recalculated.
The variables in the application keep accumulating. Unless you restart tomcat, it will keep increasing.
The scope specifies the validity period of the variable.
If you put the variable in pageContext, it indicates that its scope is page, and its valid range is only in the current jsp page.
You can use this variable from placing it in pageContext to the end of the jsp page.
If you put the variable in the request, it indicates that its scope is request, and its valid range is the current request cycle.
The request cycle refers to the whole process from initiating an http request to processing the server and returning a response. In this process
The forward method redirects to multiple jsp pages. You can use this variable in these pages.
If the variable is put into the session, it indicates that its scope is session, and its valid range is the current session.
The current session refers to the process from when the user opens the browser to when the user closes the browser. This process may contain multiple
Request Response. That is to say, as long as the user is not in the browser, the server can know that these requests are initiated by a person, and the whole process is
It is called a session, and the variables put in the session can be used in all requests of the current session.
If you put the variable in the application, it indicates that its scope is application, and its effective scope is the entire application.
The entire application starts from the application and ends with the application. We didn't say "starting from the server to shutting down the server" because of a service
You may deploy multiple applications. Of course, if you close the server, all the above applications will be closed.
Variables in the application scope have the longest survival time. They can be used without manual deletion.
Different from the preceding three, the variables in the application can be shared by all users. If user a's operations modify the application
User B obtains the modified value during access. This will not happen in other scopes, such as page, request,
Sessions are completely isolated, and no matter how modified, other people's data will be affected.
We use public Object getAttribute (String name) to obtain the variable value, and use public void setAttribute
(String name, Object value) saves the variable value to the corresponding scope. For example, pageContext:
// Page
Integer countPage = (Integer) pageContext. getAttribute ("countPage ");
If (countPage = null ){
PageContext. setAttribute ("countPage", 1 );
} Else {
PageContext. setAttribute ("countPage", countPage + 1 );
}
Here, we first retrieve the integer countPage from pageContext, because the returned values are of the java. lang. Object type, so we need
Convert to the integer we need. If the obtained variable does not exist, null is returned, which is determined by countPage = null.
Whether the variable exists. If it does not exist, set it to 1. If it exists, accumulate the variable and use the setAttribute () method to modify the variable.
Put the variable value in pageContext.
Replace pageContext with request and session, and the application can operate the variables in the other three scopes.
From the column gdn_wolf