Four scopes for the Web backend note jsp: The difference between page, request, session, and application

Source: Internet
Author: User

1. Difference:

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.

Summary: when the data only needs to be useful in the next forward, use request is enough;
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, 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. 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 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.

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)

1 <spanstyle= "font-family: Microsoft Jacob Black, ' Microsoft Yahei ';"><C:setvalue= "AAA"var= "Test1"Scope= "page"/><BR><C:setvalue= "AAA"var= "Test2"Scope= "Request"/><BR><C:setvalue= "AAA"var= "Test3"Scope= "Session"/><BR><C:setvalue= "AAA"var= "Test4"Scope= "Application"/><BR><BR><Jsp:forwardpage= "master.jsp"/>Here is a description<BR></span>

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.

Reference Original: Http://my.oschina.net/chape/blog/124128#OSC_h1_3

Four scopes for the Web backend note jsp: The difference between page, request, session, and application

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.