The four scope pagecontext,request,session,servletcontext of the Java EE (turn)

Source: Internet
Author: User

Turn from: (HTTP://WWW.5YCODE.COM/63)

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 at which 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 can be accessed throughout the life cycle of the Web application. In the JSP, there are 4 scopes, as shown below.

One, page range –pagecontext domain

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. You can call the GetAttribute () method of this implicit object to access an object with this scope type (the PageContext object also provides a getattribute method for accessing other scope objects), PageContext. The PageContext object itself is also part of the page range. When the _jspservice () method of the Servlet class executes, a reference to the object belonging to the page range is discarded. A page-scoped object that is created every time a client requests a JSP page, after the page sends a response back to the client, or the request is forwarded (forward) to another resource, is deleted.

Second, request scope –request domain

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 the object with this scope 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 // Gets the value of the page form form that has the Name property of "name" of 2, Getparameternames () return enumeration // get all forms with the name attribute submitted by the page (including button)3, Getparametervalues (String name) return string[] // method Gets the value of the name attribute in all form forms. The method returns an array.

Three, session range –session domain

Objects with a session scope are bound to the Javax.servlet.http.HttpSession object, and you can call the GetAttribute () method of the implicit object of the session to access objects 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:

return Object    return Enumeration return Long return String return Long return int void void

Four, application range –servletcontext domain

Objects with a application scope are bound to javax.servlet.ServletContext, and you can call the GetAttribute () method of application, the implied object, to access objects with this range type. During a Web application run, all pages can access objects within this scope.

The main methods of application objects are:

return Object return Enumeration3, Getinitparameter (string name)4, Getservletinfo ()5, SetAttribute (string Name, Object object)

Configuring initialization parameters in Web. xml

Configure under <web-app>

< Context-param >   < Param-name >yxkong</param-name>   <Param-value > Fish air </param-value></Context-param  >

Reading parameters in Web. xml

ServletContext sc=this. Getservletcontext (); Sc.getinitparameter ("Yxkong"); Sc.getinitparameternames ();

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", "Yxkong");   Out.println ("test1.jsp:");   Out.println (Pagecontext.getattribute ("name"));   Out.println ("<p>");   Pagecontext.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:yxkong

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", "Yxkong");  Out.println ("test1.jsp:");  Out.println (Request.getattribute ("name"));  Out.println ("<p>");  Pagecontext.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:yxkong

Test2.jsp:yxkong

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", "Yxkong"); %>

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:yxkong

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", "Yxkong"); %>

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:yxkong

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.

The four scope pagecontext,request,session,servletcontext of the Java EE (turn)

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.