Javaweb Learning notes 9--jsp attribute range

Source: Internet
Author: User
Tags ranges tomcat server

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

description

1

public void setattribute (String  name,object value)

set properties

2

public object  getattribute (string name)

Get Properties

3

public  Void removeattribute (string name)

Delete properties

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", "Aloof Wolf");  Set Property 6     pagecontext.setattribute ("date", new Date ());//Set Property 7     //Note: The two properties set here are named name and date, respectively, and these are the data of the string type. But the corresponding attribute values MLDN and new date are two values that are not string types, but two types of data of type object. 8%> 9 <%10     //Get Set properties one by one     String refName = (string) pagecontext.getattribute ("name");     A//because the obtained value is of type object, you must use string to force a downward transformation into a string of type     refdate = (date) pagecontext.getattribute ("date ")%>15 

The results of the program run as follows:

  

This shows that the PageContext Range property set on this page is actually available on this page, using the jump statement below to see if the property can 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", "Aloof pale Wolf");  5     Pagecontext.setattribute ("date", new Date ()), 6%>7 <%--using 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 

In the above program pagescopedemo02.jsp only set two properties, jump to pagescopedemo03.jsp and then in pagescopedemo03.jsp to take the page property in the pagescopedemo02.jsp settings. At this point, the results are as follows:

  

Server-side jumps are used, but the found content is not available, proving that the page range properties can only be obtained on this page, and jumps to other pages cannot 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 property scope indicates that the request attribute is valid for a single server jump, as long as it is a server jump, which can be passed down.

  

Example: requestscopedemo01.jsp

1 <% @page contenttype= "Text/html;charset=utf-8"%>2 <% @page import= "java.util.*"%>3 <%4     Request.setattribute ("name", "Aloof pale Wolf");  5     Request.setattribute ("date", new Date ()), 6%>7 <%--using 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     // Gets the property of the Requestscopdemo01.jsp setting 5     String refName = (string) request.getattribute ("name");  6     Date refdate = (date) request.getattribute ("date"), 7%>8 

The results of the operation are as follows:

  

From the running results, the program jumps, but compared to the page range, the content can continue to pass down, that is, the first page set of properties jump to the second page, the second page can still get the first page set properties.

If you now have a third page, you can also continue to pass backward

Example: Modifying requestscopedemo02.jsp

1 <% @page contenttype= "Text/html;charset=utf-8"%>2 <% @page import= "java.util.*"%>3 <%--using JSPs: 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     // Gets the property of the Requestscopdemo01.jsp setting 5     String refName = (string) request.getattribute ("name");  6     Date refdate = (date) request.getattribute ("date"), 7%>8 

The above results are still accessible, but if the hyperlink is used at this time, the property cannot continue to be passed down.

Example: Modifying requestscopedemo03.jsp

1 <% @page contenttype= "Text/html;charset=utf-8"%> 2 <% @page import= "java.util.*"%> 3 <% 4     // Gets the property of the Requestscopdemo01.jsp setting 5     String refName = (string) request.getattribute ("name");   6     Date refdate = (date) request.getattribute ("date"), 7%> 8 

At this time, using a hyperlink jump, once the jump, the address bar changes, so this kind of jump can also be called the client jump.

requestscopedemo04.jsp

1 <% @page contenttype= "Text/html;charset=utf-8"%>2 <% @page import= "java.util.*"%>3 <%4     // Gets the property of the Requestscopdemo01.jsp setting 5     String refName = (string) request.getattribute ("name");  6     Date refdate = (date) request.getattribute ("date"), 7%>8 

Operation Result:

The result of the requestscopedemo04.jsp page display is null. This shows that the properties that are set on requestscopedemo01.jsp This page are hyperlinked when the client jumps to another page, the other page cannot get the properties set in Requestscopedemo01.jsp.

If you want to further expand the attribute range, you can use the Session scope property

3.3. Session attribute Range

The properties of the session settings can be obtained regardless of how they jump. Of course, thesession is only for one user

  

After you set the properties on the first page, jump (server jump/client jump) to other pages, other pages can still get the properties set on the first page.

The following code to observe the scope of the session property

Example: sessionscopedemo01.jsp

1 <% @page contenttype= "Text/html;charset=utf-8"%>2 <% @page import= "java.util.*"%>3 <%4     // The properties set at this time are only able to get 5     session.setattribute ("name", "Aloof Wolf") on any page related to this page;  Set Property 6     session.setattribute ("date", new Date ()), 7%>8 <%--use server-side jump--%>9 <jsp:forward page= "/ Sessionscopedemo02.jsp "/>

The server-side jump is used here

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 

This is a client-side jump that uses hyperlinks

The result of running the program sessionscopedemo01.jsp is 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%>

Click the hyperlink sessionScopeDemo03, jump to the sessionscopedemo03.jsp this page, this time the program running results are as follows:

  

This means that even with a client-side jump, the session properties set on the first page can still be obtained on the other page. However, if, at this point, a new browser is opened, sessionscopedemo03.jsp will not be able to get the properties of the session object set in Sessionscopedemo01.jsp because the session only retains one person's information.

If a property wants to be accessible to all users, the last attribute range can be used: application range.

3.4. Application Attribute Range

  

Because the Application property scope is a property set on the server, any user can browse to this property once it is set.

The following code to observe the application attribute range

Example: applicationscopedemo01.jsp

1 <%@ page contenttype= "TEXT/HTML;CHARSET=GBK"%>2 <%@ page import= "java.util.*"%>3 <%4     // At this time set the properties of any user can get 5     application.setattribute ("name", "Aloof pale Wolf");  Set Property 6     application.setattribute ("date", new Date ()), 7%>8 

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 

See how the page works:

  

Open multiple browser windows, When you run applicationscopedemo02.jsp, you can show the results as shown, because the property scope is set on the server, so any user connected to this server can get this property, of course, if the server is down, this property will definitely disappear.

If the tomcat server is shut down and then restarted, when you open the browser window to run applicationscopedemo02.jsp, the results are as follows:

  

Note: If too many application properties are set on the server, the performance of the server is affected.

3.5. Further additions to the PageContext attribute range

The four attribute ranges previously explained are actually set by the PageContext attribute range. Open the description document where the PageContext is located.

  

The PageContext class inherits the Jspcontext class, and the SetAttribute method is defined in the Jspcontext class, as follows:

Public abstract void SetAttribute (String name,object value,int scope)

There is a scope integer variable in this method that represents the extent to which an attribute is saved.

  

The PageContext class inherits the Jspcontext class, so an 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 scope parameter of the following int type, this parameter defaults to Page_scope, at which point the setattribute () method sets the page property range, If the int type parameter passed over is Request_scope, then the SetAttribute () method sets the request attribute range, and the same is true for the scope parameter passed to Session_scope and Application_ Scope, it means that the setattribute () method sets the session property scope and the application attribute range.

The following code observes the effect of these four attribute range constants, as follows: request as an example

Example: pagescopedemo04.jsp

1 <% @page contenttype= "TEXT/HTML;CHARSET=GBK"%>2 <% @page import= "java.util.*"%>3 <%4     Pagecontext.setattribute ("name", "Aloof pale Wolf", pagecontext.request_scope);  Set the property and indicate the property range 5     pagecontext.setattribute ("date", new Date (), pagecontext.request_scope);//Set the property and indicate the property 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     the property 5 String refName = (string) request.getattribute ("name"); 6     Date refdate = (date) Request.getattribute ("date"); 7     ///You can also get properties using the PageContext object, as long as the object's property range is specified on fetch 8     String refName2 = (string) pagecontext.getattribute ("name", Pagecontext.request_scope); 9     Date RefDate2 = (date) pagecontext.getattribute ("date", pagecontext.request_scope);%>11 Get properties using the Request object: 

Operation Result:

  

From the running result you can see that the property scope set by the PageContext object call SetAttribute () method in pagescopedemo04.jsp is the property scope of the request, so when this method is called, The scope constant request_scope of an int type is passed in, and the function of this Request_scope property scope constant is to tell the PageContext object that the range of properties to be set now is the property range of the request. So pagescopedemo05.jsp can use the Request.getattribute () method directly on this page to get the properties set in Pagescopedemo04.jsp.

Four, JSP four kinds of attribute range of use occasions

1, Request: If the customer requests to the server, the resulting data, the user read it useless, like this data exists in the request domain, such as news data, belong to the user read it useless.
2, session: If the customer sends a request to the server, the resulting data, the user ran out and so on for a while also useful, such as data exist in the session domain, like shopping data, users need to see their own shopping information, and wait a while, but also to use this shopping data checkout.
3, Application (ServletContext): If the customer requests to the server, the resulting data, the user ran out, but also to other users, such as this data exists in the application (servletcontext) domain, like chat data.

Javaweb Learning notes 9--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.