Javaweb Learning Summary (18)--jsp attribute range

Source: Internet
Author: User
Tags ranges


the so-called attribute range is the range of save that can be accessed after a property is set, after how many other pages are still accessible. One, JSP attribute scope


There are four attribute ranges available in the JSP, and four attribute ranges refer to the following four types:


    • Current Page : A property can only be obtained in one page, jump to another page cannot get
    • one server request : A property set on a page, as long as the server jumps, the page after the jump can continue to get.
    • One session : The content of a user setting, as long as it is relevant to the user's page can be accessed (a session represents a person, this person set things as long as this person does not go, it will still be effective)
    • Context : Properties set on the entire server, accessible to everyone
Second, the operation method of the attribute


Since four attribute ranges are available in JSPs, the following property action methods are included in the four attribute ranges.


No.

Method

Describe

1

public void SetAttribute (String name,object value)

Setting properties

2

public Object GetAttribute (String name)

Get Properties

3

public void RemoveAttribute (String name)

Delete Property


The operation of the property is nothing more than adding, getting, and deleting this several operations.



The word attribute means "attribute", SetAttribute (String name,object value) from the combination of words to know is this method is to set the property, set the property name and the value of the property, first name (name) is a string type, and the value is of type object, because the value is of type object, which means that any type of data can be set as a value because all classes inherit from the object type. Therefore, the property value can be any type of data when it is set. The GetAttribute (string name) method is a property that is obtained based on the name of the property, and the RemoveAttribute (string name) method deletes the property based on the name of the property.


Three, the JSP four kinds of attribute scope specific Introduction 3.1, page attribute scope (PageContext)


The page property range is relatively well understood: the properties that are set on a page are not accessible when you jump to another page. however, when using the page property scope, it is important to note that although it is customary to refer to a page Range property as a range of pages, the actual operation is done using the PageContext built-in object.



PageContext Attribute scope operation flowchart






PageContext, literally defined, can be found to represent a page (the context) that represents all the content in a page.



From the operational flowchart, the properties set on the first page are not available on the second page after the server-side jump to the second page, just like there is a pen on the table now sitting, but once you leave this table and sit on another table, the pen is gone.



The following code to observe the properties of this range



Example: pagecontextdemo01.jsp



Set two properties on a page

1 <% @ page contentType = "text / html; charset = UTF-8"%>
 2 <% @ page import = "java.util. *"%>
 3 <%
 4 // The properties set at this time can only be obtained on this page
 5 pageContext.setAttribute ("name", "Solitary Pride Wolf"); // Set attribute
 6 pageContext.setAttribute ("date", new Date ()); // Set attributes
 7 // Note: The names of the two attributes set here are name and date, respectively. These two are string data, but the corresponding attribute values MLDN and new Date are not string types, but rather Two types of data.
 8%>
 9 <%
10 // Get the set attributes
11 String refName = (String) pageContext.getAttribute ("name");
12 // Because the obtained value is of type Object, you must use String to force down conversion and convert to String type
13 Date refDate = (Date) pageContext.getAttribute ("date");
14%>
15 <h1> Name: <% = refName%> </ h1>
16 <h1> Date: <% = refDate%> </ h1>
The results of the program run are as follows:



This shows that the pageContext range property set on this page can be obtained on this page. Use the jump statement below to see if the property can still be obtained after the jump.

Example: pageScopeDemo02.jsp

1 <% @ page contentType = "text / html; charset = UTF-8"%>
2 <% @ page import = "java.util. *"%>
3 <%
4 pageContext.setAttribute ("name", "Lonely Wolf");
5 pageContext.setAttribute ("date", new Date ());
6%>
7 <%-use jsp: forward tag for server-side jump-%>
8 <jsp: forward page = "/ pageScopeDemo03.jsp" />
Example: pageScopeDemo03.jsp

1 <% @ page contentType = "text / html; charset = UTF-8"%>
2 <% @ page import = "java.util. *"%>
3 <%
4 String refName = (String) pageContext.getAttribute ("name");
5 Date refDate = (Date) pageContext.getAttribute ("date");
6%>
7 <h1> Name: <% = refName%> </ h1>
8 <h1> Date: <% = refDate%> </ h1>
PageThe pageScopeDemo02.jsp in the above program only sets two properties. After jumping to pageScopeDemo03.jsp, take the page property set in pageScopeDemo02.jsp in pageScopeDemo03.jsp. At this point, the results are as follows:



Uses server-side jump, but found that the content can not be obtained, prove that the attributes of the page range can only be obtained on this page, jump to other pages can not be obtained. If you now want to jump to another page and still get it, you can expand the attribute range and use the request attribute range.

3.2, request attribute range
The request attribute range indicates that it is valid in a server jump. As long as it is a server jump, the set request attribute can be passed on all the time.



 Example: requestScopeDemo01.jsp

1 <% @ page contentType = "text / html; charset = UTF-8"%>
2 <% @ page import = "java.util. *"%>
3 <%
4 request.setAttribute ("name", "Lonely Wolf");
5 request.setAttribute ("date", new Date ());
6%>
7 <%-use jsp: forward tag for server-side jump-%>
8 <jsp: forward page = "/ requestScopeDemo02.jsp" />
 Example: requestScopeDemo02.jsp

1 <% @ page contentType = "text / html; charset = UTF-8"%>
2 <% @ page import = "java.util. *"%>
3 <%
4 // Get the attributes set by requestScopdemo01.jsp
5 String refName = (String) request.getAttribute ("name");
6 Date refDate = (Date) request.getAttribute ("date");
7%>
8 <h1> Name: <% = refName%> </ h1>
9 <h1> Date: <% = refDate%> </ h1>
The results are as follows:



From the running result, the program jumps, but compared to the page range, the content can continue to be passed down, that is, the properties set on the first page can still be transferred on the second page after jumping to the second page. Get the properties set on the first page.

If there is a third page now, you can continue to pass back

Example: Modify requestScopeDemo02.jsp

1 <% @ page contentType = "text / html; charset = UTF-8"%>
2 <% @ page import = "java.util. *"%>
3 <%-Use jsp: forward tag for server-side jump-%>
4 <jsp: forward page = "/ requestScopeDemo03.jsp" />
Example: requestScopeDemo03.jsp

1 <% @ page contentType = "text / html; charset = UTF-8"%>
2 <% @ page import = "java.util. *"%>
3 <%
4 // Get the properties set by requestScopdemo01.jsp
5 String refName = (String) request.getAttribute ("name");
6 Date refDate = (Date) request.getAttribute ("date");
7%>
8 <h1> Name: <% = refName%> </ h1>
9 <h1> Date: <% = refDate%> </ h1>
The above results can still be accessed, but if, at this time, a hyperlink is used to transfer, the attribute cannot be passed down.

Example: Modify requestScopeDemo03.jsp

 1 <% @ page contentType = "text / html; charset = UTF-8"%>
 2 <% @ page import = "java.util. *"%>
 3 <%
 4 // Get the properties set by requestScopdemo01.jsp
 5 String refName = (String) request.getAttribute ("name");
 6 Date refDate = (Date) request.getAttribute ("date");
 7%>
 8 <h1> Name: <% = refName%> </ h1>
 9 <h1> Date: <% = refDate%> </ h1>
10 <h1>
11 <%-Using a hyperlink to jump, this is a client-side jump, the URL address will change-%>
12 <a href="${pageContext.request.contextPath}/requestScopeDemo04.jsp"> Jump to requestScopeDemo04.jsp </a>
13 </ h1>
At this time, a hyperlink jump is used. Once the jump occurs, the address bar changes, so this jump can also be called a client jump.

requestScopeDemo04.jsp

1 <% @ page contentType = "text / html; charset = UTF-8"%>
2 <% @ page import = "java.util. *"%>
3 <%
4 // Get the attributes set by requestScopdemo01.jsp
5 String refName = (String) request.getAttribute ("name");
6 Date refDate = (Date) request.getAttribute ("date");
7%>
8 <h1> Name: <% = refName%> </ h1>
9 <h1> Date: <% = refDate%> </ h1>
operation result:

 

The requestScopeDemo04.jsp page displays null. This shows that the properties set in the requestScopeDemo01.jsp page are redirected to other pages via a client such as a hyperlink, and other pages cannot obtain the properties set in requestScopeDemo01.jsp.

If you want to further expand the scope of attributes, you can use the session scope attribute

 3.3, session attribute scope
The properties set by session can be obtained no matter how to jump. Of course, the session is only for one user



The attributes set on the first page, after jumping (server jump / client jump) to other pages, other pages can still get the attributes set on the first page.

The following code to observe the scope of the session attribute

Example: sessionScopeDemo01.jsp

1 <% @ page contentType = "text / html; charset = UTF-8"%>
2 <% @ page import = "java.util. *"%>
3 <%
4 // The properties set at this time can only be obtained in any page related to this page
5 session.setAttribute ("name", "Lonely Wolf"); // Set attribute
6 session.setAttribute ("date", new Date ());
7%>
8 <%-use server-side jump-%>
9 <jsp: forward page = "/ sessionScopeDemo02.jsp" />
Here is the server-side jump

sessionScopeDemo02.jsp

 1 <% @ page contentType = "text / html; charset = UTF-8"%>
 2 <% @ page import = "java.util. *"%>
 3 <%
 4 String refName = (String) session.getAttribute ("name");
 5 Date refDate = (Date) session.getAttribute ("date");
 6%>
 7 <h1> Name: <% = refName%> </ h1>
 8 <h1> Date: <% = refDate%> </ h1>
 9 <%-client-side jumps using hyperlinks-%>
10 <h1> <a href="${pageContext.request.contextPath}/sessionScopeDemo03.jsp"> sessionScopeDemo03 </a> </ h1>
客户 端 Here is a client-side jump using a hyperlink

Run the program sessionScopeDemo01.jsp as follows:



sessionScopeDemo03.jsp

1 <% @ page contentType = "text / html; charset = UTF-8"%>
2 <% @ page import = "java.util. *"%>
3 <%
4 String refName = (String) session.getAttribute ("name");
5 Date refDate = (Date) session.getAttribute ("date");
6%>
7 <h1> Name: <% = refName%> </ h1>
8 <h1> Date: <% = refDate%> </ h1>
Click on the hyperlink sessionScopeDemo03, and jump to the page of sessionScopeDemo03.jsp. At this time, the running result of the program is as follows:



This shows that even if the client jump is used, the session attribute set in the first page can still be obtained on other pages. However, if a new browser is opened at this time, sessionScopeDemo03.jsp must not be able to obtain the properties of the session object set in sessionScopeDemo01.jsp, because the session only retains one person's information.

If an attribute wants to be accessible to all users, you can use the last attribute scope: application scope.

3.4, application attribute scope


Because the application attribute range is an attribute set on the server, any user can browse this attribute once set.

The following code to observe the scope of the application attribute

Example: applicationScopeDemo01.jsp

1 <% @ page contentType = "text / html; charset = GBK"%>
2 <% @ page import = "java.util. *"%>
3 <%
4 // The attribute set at this time can be obtained by any user
5 application.setAttribute ("name", "Solitary Pride Wolf"); // Set attribute
6 application.setAttribute ("date", new Date ());
7%>
8 <h1> <a href="${pageContext.request.contextPath}/applicationScopeDemo02.jsp"> applicationScopeDemo02 </a> </ h1>
Example: applicationScopeDemo02.jsp

1 <% @ page contentType = "text / html; charset = GBK"%>
2 <% @ page import = "java.util. *"%>
3 <%
4 String refName = (String) application.getAttribute ("name");
5 Date refDate = (Date) application.getAttribute ("date");
6%>
7 <h1> Name: <% = refName%> </ h1>
8 <h1> Date: <% = refDate%> </ h1>
Observe the running effect of the page:



Open multiple browser windows and run applicationScopeDemo02.jsp, all of them can show the results shown, because the attribute scope is set in the server, so any user connected to this server can get this attribute, of course, if If the server is shut down, this property will definitely disappear.

If you shut down the Tomcat server and then restart it, open the browser window and run applicationScopeDemo02.jsp, the result is as follows:



Note: If too many application attributes are set on the server, it will affect the performance of the server.

3.5. Further supplements on the scope of the pageContext attribute
The four attribute ranges explained before are actually set through the pageContext attribute range. Open the documentation where pageContext is located.



PageContext class inherits JspContext class, setAttribute method is defined in JspContext class as follows:

         public abstract void setAttribute (String name, Object value, int scope)

 In this method, there is an integer variable of scope, and this variable represents the storage scope of an attribute.



The PageContext class inherits the JspContext class, so the abstract setAttribute method is implemented in the PageContext class:

1 public abstract void setAttribute (String name, Object value, int scope)
If the setAttribute () method does not write the following int type scope parameter, this parameter defaults to PAGE_SCOPE. At this time, the setAttribute () method sets the page attribute range. If the passed int type parameter scope is REQUEST_SCOPE, then this The setAttribute () method sets the request attribute range. Similarly, when the scope parameters passed are SESSION_SCOPE and APPLICATION_SCOPE, it means that the setAttribute () method sets the session attribute range and application attribute range.

The following uses code to observe the effect of these four attribute range constants. Take request as an example.

Example: pageScopeDemo04.jsp

1 <% @ page contentType = "text / html; charset = GBK"%>
2 <% @ page import = "java.util. *"%>
3 <%
4 pageContext.setAttribute ("name", "Guaolang", PageContext.REQUEST_SCOPE); // Set attribute, and indicate the attribute range
5 pageContext.setAttribute ("date", new Date (), PageContext.REQUEST_SCOPE); // Set the attribute, and indicate the attribute range
6%>
7 <jsp: forward page = "/ pageScopeDemo05.jsp" />
pageScopeDemo05.jsp

 1 <% @ page contentType = "text / html; charset = GBK"%>
 2 <% @ page import = "java.util. *"%>
 3 <%
 4 // Use the request object to get attributes
 5 String refName = (String) request.getAttribute ("name");
 6 Date refDate = (Date) request.getAttribute ("date");
 7 // You can also use the pageContext object to get attributes, as long as you specify the scope of the object's attributes
 8 String refName2 = (String) pageContext.getAttribute ("name", PageContext.REQUEST_SCOPE);
 9 Date refDate2 = (Date) pageContext.getAttribute ("date", PageContext.REQUEST_SCOPE);
10%>
11 Use the request object to get attributes:
12 <h1> Name: <% = refName%> </ h1>
13 <h1> Date: <% = refDate%> </ h1>
14 Use the pageContext object to get attributes:
15 <h1> Name: <% = refName2%> </ h1>
16 <h1> Date: <% = refDate2%> </ h1>
operation result:



From the running results, you can see that the pageScopeDemo04.jsp uses the pageContext object to call the setAttribute () method to set the attribute range is the request attribute range, so when calling this method, an int type scope constant REQUEST_SCOPE is passed in The role of this REQUEST_SCOPE attribute range constant is to tell the pageContext object that the attribute range to be set is the attribute range of the request, so the pageScopeDemo05.jsp page can directly use request.getAttribute (); method to get the attribute set in pageScopeDemo04.jsp.

Fourth, the use occasions of the four attribute ranges of jsp
1. request: If the client sends a request to the server, the data generated by the user will be useless after reading it. Data like this exists in the request field, like news data, which belongs to the user after useless.
2. Session: If the client sends a request to the server, the data generated by the user is still useful after the user runs out. Data like this is stored in the session field. Like shopping data, the user needs to see his shopping information and wait for a while. To checkout with this shopping data.
3. application (servletContext): If the client sends a request to the server, the generated data is used up by the user, and it is also used by other users. Such data is stored in the application (servletContext) domain, like chat data.

Javaweb learning summary (eighteen)-JSP attribute 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.