Defines the difference between the 4 scopes of the 6 JSP Pagescope, Requestscope, Sessionscope, Applicationscope

Source: Internet
Author: User

1. Difference:

1. page refers to the current screen as valid. Valid in a JSP page

2 . Request means The whole process of a request is valid, that is, from the HTTP request to the end of the server processing, the entire process of returning the response, stored in HttpServletRequest Object Forward way to jump multiple JSPs. You can use this variable on these pages.

3 . session is user global variable, 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 HttpSession object  

4 . application is a program global variable that works on each page for each user. stored in the ServletContext object.

Summary: 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.

2, how to use?

pageThe variables in the index.jsp cannot be passed from the to the test.jsp. As soon as the page jumps, they are gone.
RequestThe variables in the forward can span two pages before and after the But as soon as you refresh the page, they recalculate.
SessionVariable has been accumulated, beginning to see the difference, as long as the browser is closed, restart the browser to access the page again, the variables in the session are recalculated. 
ApplicationThe variable keeps accumulating until you restart Tomcat, it will always get 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, and its valid range is only in the currentJSP 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.CalledThe request period, which is the entire process of returning the response from the HTTP request to the end of the server processing. 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.
Called The 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 app is started from the app to the end of the app. Not saying "Start from server to server shut down" is because a server may deploy multiple applications, of course you shut down the server, you will be all the above applications are closed. application scope, they have the longest surviving time, If you do not delete them manually, they are always available.  
      Unlike the three above, 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.

3, the book explains

Only important statements are extracted here:

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.

3.1 for an illustration

A website mywebsite that contains 7 JSP pages, master.jsp, top.jsp, main.jsp, foot.jsp, login.jsp, success.jsp, and news.jsp, respectively. Master.jsp page through the <include> directive contains top.jsp, main.jsp, foot.jsp three pages, when the user through the login.jsp login success, enter the Success.jsp page and then through this page < The Forward> tab jumps to the master.jsp page.

We add the following code to the Success.jsp page (the JSTL tag is used here)

<C:Set value="AAA"Var="Test1" scope= "page"/>
<C:Set value="AAA"var= "test2" scope= "Request"/>
<c: Set Value= "AAA" var=" test3 "scope=" session "/>
<c:set Value= "AAA" var= "test4" scope= "Application"/

<jsp:forward page= "master.jsp"/> here is a description of

1. Variable test1 is only valid within success.jsp;
2. Variable test2 is effective in success.jsp, master.jsp, top.jsp, main.jsp and foot.jsp;
3. Variable test3 is valid on 7 pages (including login.jsp and news.jsp);
4. Variable test4 is available throughout the site, currently 7 pages (including login.jsp and news.jsp).

Test3 and test4 effective range, what is the difference?

Note that if there is another user accessing the master.jsp page at this point, then TEST3 is invalid relative to the user, and Test4 is valid. Or when the first logged-on user closes the browser and then accesses master.jsp again, TEST3 is invalid and test4 is valid.

Original address: http://www.cnblogs.com/ellisonDon/archive/2012/10/29/2744872.html

Defines the difference between the 4 scopes of the 6 JSP Pagescope, Requestscope, Sessionscope, Applicationscope

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.