Page Request Session Application Range

Source: Internet
Author: User

Objects in JSP pages, including user-created objects (for example, JavaBean objects) and JSP implicit objects, have a range attribute. The scope defines the time period,

These objects can be accessed in which JSP page. For example, a session object can be accessed in multiple pages during a conversation. Application objects throughout the web should be

can be accessed during the lifetime of the program. In the JSP, there are 4 scopes, as shown below.

1. Page Range

An object with a page range is bound to the Javax.servlet.jsp.PageContext object. Objects within this range can only be accessed in the page where the object is created. Can be adjusted

Use the GetAttribute () method of this implied object to access objects with this range type (the PageContext object also provides access to the PageContext object.

GetAttribute method), the PageContext object itself belongs to the page range. When the _jspservice () method of the Servlet class finishes executing, the object belonging to the page range

The reference will be discarded. A page-scoped object that is created every time a client requests a JSP page, sends a response back to the client in the page, or a request is forwarded (forward) to its

His resources were deleted after that.

2. Request Scope

The object with the request scope is bound to the Javax.servlet.ServletRequest object, and you can call the GetAttribute () method of the implied object of request to access

An object with this range type. Objects in this range can be accessed by invoking the forward () method to the page that is being moved or by calling the include () method contained in the page.

It is important to note that because the request object is different for each client request, the objects in this range are recreated and deleted for each new request.

The main method of the Request object:

1, GetParameter (string name) return string

2, Getparameternames () return enumeration

3, Getparametervalues (String name) return string[]

3.session Range

An object with a session scope is bound to the Javax.servlet.http.HttpSession object, and the GetAttribute () method of the implied object of the session can be called to access

An object with this range type. The JSP container creates a HttpSession object for each session, during which the object within the session scope can be accessed.

The main method of the Session object:

1, getattribute (String name) return Object

2, Getattributenames () return enumeration

3, GetCreationTime () return long

4, GetId () return String

5, Getlastaccessedtime () return long

6, Getmaxinactiveinterval () return int

7, RemoveAttribute (String name) void

8, SetAttribute (String name, java.lang.Object value) void

4.application Range

An object with a application scope is bound to the Javax.servlet.ServletContext, and you can call the GetAttribute () method of the Application object to access

An object with this range type. During a Web application run, all pages can access objects within this scope.

The main methods of application objects are:

1, getattribute (String name) return Object

2, Getattributenames () return enumeration

3, Getinitparameter (String name)

4, Getservletinfo ()

5, SetAttribute (String name, Object object)

Let's look at the application of these 4 range objects in a few simple examples.

1. Test Page Range

test1.jsp

<%pagecontext.setattribute ("name", "Zhangsan"); Out.println ("test1.jsp:"); Out.println ( Pagecontext.getattribute ("name")), Out.println ("<p>");p agecontext.include ("test2.jsp");%>

test2.jsp

<%out.println ("test2.jsp:"); Out.println (Pagecontext.getattribute ("name"));%>

To access test1.jsp, you will see the following output:

Test1.jsp:zhangsan

Test2.jsp:null

Description The property saved in the PageContext object has a page range and is accessible only on the same page.

2. Test request Scope

Modify the test1.jsp and test2.jsp as shown below.

test1.jsp

<%request.setattribute ("name", "Zhangsan"), Out.println ("test1.jsp:"); Out.println (Request.getattribute ("name" ); Out.println ("<p>");p agecontext.include ("test2.jsp");%>

test2.jsp

<%out.println ("test2.jsp:"); Out.println (Request.getattribute ("name"));%>

To access test1.jsp, you will see the following output:

Test1.jsp:zhangsantest2.jsp:zhangsan

Indicates that the property saved in the Request object has a request scope and can access objects within that range during the request object's survival.

Will Pagecontext.include ("test2.jsp"); This sentence comments up, first visit test1.jsp, and then visit test2.jsp, you can see the following output:

Test2.jsp:null

This is because the client started a new request.

3. Test Session Range

Modify the test1.jsp and test2.jsp as shown below.

test1.jsp

<%session.setattribute ("name", "Zhangsan");%>

test2.jsp

<%out.println ("test2.jsp:"); Out.println (Session.getattribute ("name"));%>

To access the test2.jsp in the same browser window first, you can see the following output: test1.jsp

Test2.jsp:zhangsan

Describes the properties that are stored in the session object that have a session scope, during which you can access objects within the scope.

If we close the browser, reopen the browser window, and access test2.jsp after we have finished test1.jsp, we will see the following output:

Test2.jsp:null

This is because the client and the server start a new session.

4. Test Application Range

Modify the test1.jsp and test2.jsp as shown below.

test1.jsp

<%application.setattribute ("name", "Zhangsan");%>

test2.jsp

<%out.println ("test2.jsp:"); Out.println (Application.getattribute ("name"));%>

First visit test1.jsp, then close the browser, then open the browser window, access to test2.jsp, you can see the following output:

Test2.jsp:zhangsan

Explains that the properties that are stored in the Application object have a application scope and can access objects within this scope while the Web application is running.


Page Request Session Application Range

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.