Nine built-in objects and four attribute ranges in JSP, nine built-in objects in jsp

Source: Internet
Author: User

Nine built-in objects and four attribute ranges in JSP, nine built-in objects in jsp

Nine built-in objects and four attribute ranges in JSP

Generally, an object can be called only when it is instantiated, while the built-in JSP object can be called directly without instantiation.

There are a total of 9 corresponding to the following table:

Serial number

Object

Type

1

PageContext

Javax. servlet. jsp. PageContext

2

Request

Javax. servlet. http. HttpServletRequest

3

Response

Javax. servlet. http. HttpServletResponse

4

Session

Javax. servlet. http. HttpSession

5

Application

Javax. servlet. ServletContext

6

Config

Javax. servlet. ServletConfig

7

Out

Javax. servlet. jsp. jspWriter

8

Page

Java. lang. Object

9

Exception

Java. lang. Throwable

Four Scopes: the objects on the jsp page, including the objects created by the user (such as javaBean objects) and the hidden objects in the JSP, all have a range attribute. The range defines the time at which these objects can be accessed on which JSP page. For example, a session object can be accessed on multiple pages during a session. The application object can be accessed throughout the lifecycle of the Web application.

1. page range

Objects with a page range are bound to the javax. servlet. jsp. PageContext object. Objects in this range can only be accessed on the page where the object is created. You can call the getAttribute () method of the implicit object pageContext to access objects with this range type (the pageContext object also provides the getAttribute Method for accessing other range objects ), the pageContext object also belongs to the page range. When the Servlet class's _ jspService () method is executed, references to objects within the page range will be discarded. Objects within the page range are created each time the client requests a JSP page and deleted after the page sends an echo response or request to the client that is forwarded to another resource.

For example:

<Body> <% pageContext. setAttribute ("page", "this is the pageContext test"); %> <% = pageContext. getAttribute ("page") %> <jsp: forward page = "two. jsp "> </jsp: forward> </body>

The Code on the two. jsp receiving page is:

<%=pageContext.getAttribute("page") %> 

After running, the result shows null, which proves that the page range is only limited to the current page. No data will be transferred to another interface whether it is redirected from the server or the client.

2. request range

Objects with a request range are bound to the javax. servlet. ServletRequest object. You can call the getAttribute () method of the implicit Object request to access objects with this range type. Objects in this range can be accessed on pages that call the forward () method to turn to or pages that call the include () method. Note that because the request object is different for each customer request, you must re-create and delete the objects in this range for each new request. That is to say, after a request is redirected to the server or even to multiple server jumps, we can obtain the attribute value set by the request. However, the client jumps to the request and cannot obtain the value. For example:

<% Request. setAttribute ("name", ""); %> <jsp: forward page = "two. jsp"> </jsp: forward>

The Code on the two. jsp receiving page is:

<%=request.getAttribute("name") %> 

After running the code, you can find the link on the two. jsp page. This is a server jump. What about the client jump?

Set <jsp: forward page = "two. jsp"> </jsp: forward>

Remove <a href = "two. jsp "rel =" external nofollow "> jump </a> code. Click the jump and the obtained content is null.

3. session range

Objects with a session range are bound to the javax. servlet. http. HttpSession object. You can call the getAttribute () method of the implicit session object to access objects with this range type. The JSP Container creates an HttpSession object for each session. During the session, you can access objects within the session range. That is to say, you can get the data from the client or the server, as long as the browser is not restarted, this is also the most used in our actual development.

Example:

<% Session. setAttribute ("name", "This is a session test"); %> <a href = "two. jsp "rel =" external nofollow "> redirect </a> <% session. setAttribute ("name", "This is a session test"); %> <jsp: forward page = "two. jsp "> </jsp: forward>

The Code on the two. jsp receiving page is:

<%=session.getAttribute("name") %> 

We can find that the set data can be received on other pages, regardless of the client or server jump.

4. application Scope

Objects with application scope are bound to javax. servlet. ServletContext. You can call the getAttribute () method of the implicit object of application to access objects with this range type. During the Web application program, all pages can access objects in this range. In other words, if you set properties, you can obtain the attribute value even if you restart the browser, unless you restart the server. This range is not used as an example.

In actual development, the most common attribute ranges in the preceding four fields are request and session. Generally, the request range attribute is used only for the transfer of attributes of the same function. The session range is mainly used for login.

The preceding four attribute ranges are actually set through pageContext. In pageContext, The setAttribute () method is actually overloaded:

1,public abstract void setAttribute(java.lang.String name, java.lang.Object value), 2,public abstract void setAttribute(java.lang.String name,java.lang.Object value,int scope) 

The second method has an int scope parameter compared to the first method. In fact, this parameter is used to specify the attribute range.

1, APPLICATION_SCOPE, 2, PAGE_SCOPE, 3, SESSION_SCOPE, 4, REQUEST_SCOPE

In other words, we can set four attribute ranges through this method. That is to say, we can use pageContext to set the range of the preceding four attributes.

For example, when we use session, we can write as follows:

<% @ Page contentType = "text/html; charset = UTF-8" %> <% pageContext. setAttribute ("name", "Xiong jiutian", PageContext. SESSION_SCOPE); %> <a href = "two. jsp "rel =" external nofollow "> jump </a>

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

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.