Four Scopes of JSP: Page, request, session, and application.

Source: Internet
Author: User

1. Simply put, page refers to the current page. Valid in a JSP page
2. Request refers to the whole process of returning a response from the HTTP request to the server processing end. In this process, the forward method is used to jump to multiple JSPs. You can use this variable on these pages.
3. The session is valid for the current session, which is closed from the browser to the browser.
4. Application it is effective for the entire application.
The variables in the scope have the longest survival time. If they are not manually deleted, they can always be used.

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. As long as you close the browser and restart the browser to access this page, the variables in the session will be 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, you may use the forward method to jump 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 responses. 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. The whole process is called a session and put into the session variable, it 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 one server may deploy multiple applications. Of course, if you close the server, all the above applications will be shut down.

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 variables in the application, user B obtains the modified value during access. This will not happen in other scopes. pages, requests, and sessions are completely isolated, and data of others will not be affected no matter how modified.

 

 

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

Here, only important statements are extracted:

Page: the current page requested by the user;

Request: The current component that the user requests to access, and the Web component that shares the same user request with the current Web component. For example, the requested JSP page and the page containing the <include> command and other JSP pages marked by <forward> are used;

Session: the Web component in the same http session shares it;
Application: Share the Web components used by the entire web application.

Here is an example

A website mywebsite contains seven JSP pages, which are master. JSP, top. JSP, Main. JSP, foot. JSP, login. JSP, success. JSP and news. JSP. Master. the JSP page uses the <include> command to include top. JSP, Main. JSP, foot. JSP three pages, when the user uses login. after successfully logging on to JSP, go to success. the JSP page then jumps to the master using the <forward> label on this page. JSP page.

We add the following to the success. jsp page:Code(The jstl label 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. The variable test1 is valid only in success. jsp;
2. The Test2 variable is valid in success. jsp, master. jsp, top. jsp, Main. jsp, and foot. jsp;
3. The variable test3 is valid on all 7 pages (including login. jsp and news. jsp );
4. The variable test4 is valid for the entire website, which is currently 7 pages (including login. jsp and news. jsp ).

What is the difference between test3 and test4 in the same valid range?

In fact, the difference is quite big. I just want to explain that if another user accesses the master. jsp page at this time, test3 is invalid for this user, while test4 is effective. Or when the First Login User closes the browser and re-accesses master. jsp, test3 is invalid, but test4 is valid.

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.