An analysis of object and scope attribute in JSP development

Source: Internet
Author: User
Tags include range ranges

Objects in the JSP page, including objects created by the user (for example, JavaBean objects) and the implied objects of the JSP, have a range attribute. The scope defines the time at which the objects can be accessed in which JSP pages. For example, a session object can be accessed on multiple pages during a conversation. Application objects can be accessed throughout the life cycle of a Web application. In the JSP, there are 4 ranges, as shown below.

Page Range

Objects that have page ranges are bound to the Javax.servlet.jsp.PageContext object. Objects within this scope can only be accessed in the page where the object is created. You can call the GetAttribute () method PageContext this suppressed object to access objects with this range type (the PageContext object also provides a getattribute method to access other scope objects). The PageContext object itself also belongs to the page range. When the _jspservice () method of the Servlet class completes, a reference to the object belonging to the page range is discarded. A page-scoped object that is created each time the client requests a JSP page, is deleted when the page sends back a response to the client, or when the request is forwarded (forward) to another resource.

Request Range

Objects with the request scope are bound to the Javax.servlet.ServletRequest object and can invoke the GetAttribute () method of the implied object of request to access objects with this range type. You can access an object within this scope either by calling the forward () method or by calling the included page of the Include () method. Note that because the request object is different for each client request, the object in this scope is re-created and deleted for each new request.

Session Range

An object with a session scope is bound to a Javax.servlet.http.HttpSession object, and the GetAttribute () method of the implied object of the session can be invoked to access objects with this range type. The JSP container creates a HttpSession object for each session, during which you can access objects within the session scope.

Application Range

An object with a application range is bound to Javax.servlet.ServletContext, and the GetAttribute () method application This suppressed object can be invoked to access objects with this range type. During a Web application run, all pages can access objects within that scope.

Let's look at the application of these 4 range objects with 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>");
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:zhangsan

Test2.jsp:null

Note the properties saved in the PageContext object have a page range and can only be accessed 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>");
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:zhangsan

Test2.jsp:zhangsan

Description The attributes saved in the Request object have the request scope and can be accessed during the lifetime of the request object. Will

pageContext.include("test2.jsp");

To annotate this sentence, first visit the test1.jsp, and then visit the test2.jsp, you can see the following output:

Test2.jsp:null

This is because the client has started a new request.

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.