What is the difference between page, request, session, and application?

Source: Internet
Author: User

1. Simply say page refers to the current screen. Valid in a JSP page
2.request refers to the entire process of returning a response from an HTTP request to the end of the server processing. Use the forward method to jump through multiple jsps during this process. You can use this variable on these pages.
3.Session valid range current session, from browser open to browser close this process.
4.application its effective range is the entire application.
The variables in the scope, they survive the longest, and if they are not manually deleted, they can be used all the time.






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.

Question: What is the scope of page, request, session, application?

In the book, only the 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.

Here I give an example to illustrate

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

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?

In fact, the difference is quite large, I only point out that if there is another user access to the master.jsp page, then test3 relative to this user is invalid, and test4 is effective. Or when the first logged-on user closes the browser and then accesses master.jsp again, TEST3 is invalid and test4 is valid.

What is 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.