Simple description
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 always accumulate, and at the beginning there is no difference, as long as you close the browser and restart the browser to access the page again, 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. You may use forward to jump through multiple JSP pages in this process, and you can use this variable in 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 responses. In other words, as long as the user does not close the browser, the server has the means to know that these requests are initiated by a person, the entire process is called a session, and the variables placed in the session can be used in all requests of 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 server might deploy multiple applications, and of course you shut down the server and shut down all of the applications.
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, the variables in the application can be shared by all users. If user A's operation modifies the variable in application, the User B gets the modified value when he accesses it. This will not happen in any other scope, page, request, session is completely isolated, regardless of the modification will not affect other people's data.
4 Scope differences for JSPs
1. page refers to the current screen as valid. Valid in a JSP page
2. request refers to the whole process of a request, that is, from the HTTP request to the end of the server processing, return the entire process of the response, stored in the httpservletrequest object. In this process, you can use the forward mode to jump multiple JSPs. You can use this variable on these pages.
3. Thesession is a user global variable that is valid throughout the session. The default session expiration time is 30 minutes or the invalidate () method of HttpSession is called, as long as the page does not close until the user has been inactive causing the session to expire. stored in the HttpSession object
4.Application is a program global variable that is valid for each user page. stored in the ServletContext object. It has the longest surviving time and can be used without manual deletion.
The scope of page, request, session, application
Page: The user's current request;
Request: The current component that the user requests access to, and the Web component that shares the same user request as the current Web component. Such as: The requested JSP page and the page with the <include> directive contains the page and the <forward> tag contains other JSP pages;
Session: Share it with a Web component in the same HTTP session;
Application: The Web Component used by the entire web app shares it.
Summarize
When the data only needs to be useful in the next forward, use request is sufficient;
If the data is not only useful in the next forward, use the session.
Context, environmental information, and the like, with application.
4 Scope differences for JSPs (Pagescope, Requestscope, Sessionscope, Applicationscope)