JSP nine large built-in objects and four scopes

Source: Internet
Author: User

Request object Type Javax.servlet.ServletRequest Scope request

Response Response Object Type Javax.servlet.SrvletResponse scope Page

PageContext page Context object type Javax.servlet.jsp.PageContext Scope page

Session object type Javax.servlet.http.HttpSession scope session

Application Application Object type Javax.servlet.ServletContext scope application

Out output Object type Javax.servlet.jsp.JspWriter scope Page

Config configuration object Type Javax.servlet.ServletConfig scope Page

Page object Type Javax.lang.Object Scope page

Exception exception Object type javax.lang.Throwable Scope page

The "Exception" object represents the exception object that is generated by the JSP file runtime, which cannot be used directly in a generic JSP file, but only in a JSP file that uses "<%@ page iserrorpage=" true "%>".

What is a scope

First let's look at the effect:

The process is like this, we visit index.jsp, respectively, PageContext, request, session,application variables in the four scope to accumulate. (Of course, first of all to determine whether this variable is not present, if the variable does not exist, then the variable is initialized to 1). After the calculation is complete, jump from index.jsp execution forward to test.jsp. Once again in the test.jsp, the four integers are displayed.

Judging from the results shown, we can intuitively draw the conclusion that:

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.

SCOPE Specifies the validity period of a 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 are completely isolated, regardless of the modification will not affect other people's data.

JSP nine large built-in objects and four scopes

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.