In-depth analysis of the four fields (attributes) of Java Web Item16 -- jsp

Source: Internet
Author: User

In-depth analysis of the four fields (attributes) of Java Web Item16 -- jsp

Jsp has four fields. The scope ranges from small to large, including the page domain, request domain, session domain, and application (servletContext) domain. The fields can also be called attribute ranges. The so-called attribute range is the storage range that can be accessed after a property is set and how many other pages can be passed.

I. JSP attribute Scope

JSP provides four property ranges, which refer to the following:

Page Domain -- Current page:A property can only be obtained on one page. A request domain cannot be obtained when you jump to another page -- One server request:If the property set in a page passes the server jump, the page after the jump can continue to be obtained. Session Domain -- One session:The content set by a user can be accessed on any page related to this user (a session indicates a person. The content set by this person is still valid if this person does not leave) application -- Context:All attributes set on the server can be accessed. 2. Attribute Operation Method

Since JSP provides four attribute ranges, the four attribute ranges contain the following attribute operation methods.

Attribute operations include adding, obtaining, and deleting attributes.

WordAttributeIt means "attribute ",setAttribute(String name,Object value)From the word combination, we can know that this method is to set attributes, set the attribute name and attribute value, the name (name) is of the String type, and the value (value) it is of the Object type. Because the value is of the Object type, this indicates that data of any type can be set as the value, because all classes are inherited from the Object type. Therefore, the attribute value can be any type of data.getAttribute(String name)The method is to obtain the Attribute Based on the attribute name,removeAttribute(String name)You can delete an Attribute Based on its name.

Iii. Details about four attributes of JSP 3.1 page attribute range (pageContext)

The page attribute range is relatively easy to understand: the attributes set on a page cannot be accessed when you jump to other pages. However, when using the page attribute range, you must note that although you are used to calling the page range attribute a page range, the pageContext built-in object is used in actual operations.

PageContext attribute range operation Flowchart

    

From the literal definition, pageContext can be found to indicate the Context of a page, which can represent all the content of a page.

From the operation flow chart, after the attribute set on the first page is redirected to the second page on the server side, the attribute set on the second page cannot be obtained, it is like having a pen on the desk where you are sitting, but once you leave the desk and sit down on another desk, there will be no pen.

The following code observes the attributes of this range.

Example: pageContextDemo01.jsp

Set two properties on the page

<% @ Page contentType = "text/html; charset = UTF-8" %> <% @ page import = "java. util. * "%> <% // The property set at this time can only get pageContext on this page. setAttribute ("name", "lone wolf"); // sets the property pageContext. setAttribute ("date", new Date (); // set attributes // note: the names of the two attributes set here are name and date, respectively, these two data types are strings, but the values of the corresponding attribute values MLDN and new Date are not strings, but data of the two Object types. %> <% // Get the set property String refName = (String) pageContext. getAttribute ("name"); // because the obtained value is of the Object type, you must use String to forcibly perform downward transformation and convert it to String type Date refDate = (Date) pageContext. getAttribute ("date"); %>
Name: <% = refName %> Date: <% = refDate %>

The program running result is as follows:

  

This shows that the pageContext range attribute set on this page can be obtained on this page. The following uses a jump statement to check whether the attribute can be obtained after the jump.

Example: pageScopeDemo02.jsp

<% @ Page contentType = "text/html; charset = UTF-8" %> <% @ page import = "java. util. * "%> <% pageContext. setAttribute ("name", "lone wolf"); pageContext. setAttribute ("date", new Date (); %> <% -- use the jsp: forward tag to redirect to the server -- %>
  

Example: pageScopeDemo03.jsp

<% @ Page contentType = "text/html; charset = UTF-8" %> <% @ page import = "java. util. * "%> <% pageContext. setAttribute ("name", "lone wolf"); pageContext. setAttribute ("date", new Date (); %> <% -- use the jsp: forward tag to redirect to the server -- %>
  

In the above programpageScopeDemo02.jspOnly two attributes are set to jumppageScopeDemo03.jspAnd thenpageScopeDemo03.jspInpageScopeDemo02.jspPage attribute. The running result is as follows:

  

If server-side redirection is used, but the content cannot be obtained, it indicates that the page range attribute can only be obtained on this page, but not on other pages. If you want to jump to other pages, you can still retrieve them. You can extend the attribute range and use the request attribute range.

3.2 request attribute range

The request attribute range indicates that the request is valid for one server jump. If the request is a server jump, the request attribute can be passed continuously.

  

Example: requestScopeDemo01.jsp

<% @ Page contentType = "text/html; charset = UTF-8" %> <% @ page import = "java. util. * "%> <% request. setAttribute ("name", "lone wolf"); request. setAttribute ("date", new Date (); %> <% -- use the jsp: forward tag to redirect to the server -- %>
  

Example: requestScopeDemo02.jsp

<% @ Page contentType = "text/html; charset = UTF-8" %> <% @ page import = "java. util. * "%> <% // get the requestScopdemo01.jsp attribute String refName = (String) request. getAttribute ("name"); Date refDate = (Date) request. getAttribute ("date"); %>
Name: <% = refName %> Date: <% = refDate %>

The running result is as follows:

  

From the running result, the program jumps, but the content can be passed down in comparison with the page range, that is to say, after the attribute set on the first page jumps to the second page, the attribute set on the first page can still be obtained on the second page.

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

Example: Modify requestScopeDemo02.jsp

<% @ Page contentType = "text/html; charset = UTF-8" %> <% @ page import = "java. util. * "%> <% -- use the jsp: forward tag to redirect to the server -- %>
  

Example: requestScopeDemo03.jsp

<% @ Page contentType = "text/html; charset = UTF-8" %> <% @ page import = "java. util. * "%> <% // get the requestScopdemo01.jsp attribute String refName = (String) request. getAttribute ("name"); Date refDate = (Date) request. getAttribute ("date"); %>
Name: <% = refName %> Date: <% = refDate %>

The above results can still be accessed, but if the hyperlink is used for transmission, the attribute cannot be passed down.

Example: Modify requestScopeDemo03.jsp

<% @ Page contentType = "text/html; charset = UTF-8" %> <% @ page import = "java. util. * "%> <% // get the requestScopdemo01.jsp attribute String refName = (String) request. getAttribute ("name"); Date refDate = (Date) request. getAttribute ("date"); %>
Name: <% = refName %> Date: <% = refDate %> <% -- Use a hyperlink to redirect. This is a client jump, and the URL address will change -- %> jump to requestScopeDemo04.jsp

In this case, a hyperlink jump is used. Once the jump is completed, the address bar changes. Therefore, this jump can also be called client jump.

RequestScopeDemo04.jsp

<% @ Page contentType = "text/html; charset = UTF-8" %> <% @ page import = "java. util. * "%> <% // get the requestScopdemo01.jsp attribute String refName = (String) request. getAttribute ("name"); Date refDate = (Date) request. getAttribute ("date"); %>
Name: <% = refName %> Date: <% = refDate %>

Running result:

  requestScopeDemo04.jspThe result displayed on the page is null. This indicates thatrequestScopeDemo01.jspThe properties set on this page are hyperlinks. When a client jumps to another page, other pages cannot be obtained.requestScopeDemo01.jsp.

If you want to further expand the attribute range, you can use the session range attribute.

3.3. Range of session attributes

The attributes set by the session can be obtained no matter how they are redirected. Of course, the session is only for one user

 

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

The following code observes the range of session attributes.

Example: sessionScopeDemo01.jsp

<% @ Page contentType = "text/html; charset = UTF-8" %> <% @ page import = "java. util. * "%> <% // at this time, the set attribute can only get the session on any page related to this page. setAttribute ("name", "lone wolf"); // sets the attribute session. setAttribute ("date", new Date (); %> <% -- use server jump -- %>
  

Here, server redirection is used.

SessionScopeDemo02.jsp

<%@page contentType="text/html;charset=UTF-8"%><%@page import="java.util.*"%><%    String refName = (String)session.getAttribute("name");    Date refDate = (Date)session.getAttribute("date");%>
Name: <% = refName %> Date: <% = refDate %> <% -- Client jump using hyperlinks -- %> sessionScopeDemo03

Here we use a client jump such as a hyperlink.

Run the programsessionScopeDemo01.jspThe result is as follows:

  

SessionScopeDemo03.jsp

<%@page contentType="text/html;charset=UTF-8"%><%@page import="java.util.*"%><%    String refName = (String)session.getAttribute("name");    Date refDate = (Date)session.getAttribute("date");%>
Name: <% = refName %> Date: <% = refDate %>

Click hyperlinksessionScopeDemo03, JumpedsessionScopeDemo03.jspOn this page, the running result of the program is as follows:

  

This means that even if a client jump is used, the session attribute set on the first page can still be obtained on other pages. However, if a new browser is openedsessionScopeDemo03.jspCertainly cannot be obtainedsessionScopeDemo01.jspThe properties of the session object, because the session only retains the information of a person.

If you want all users to access an attribute, you can use the last attribute range: application range.

3.4 application attribute range

  

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

The following code observes the application attribute range.

Example: applicationScopeDemo01.jsp

<% @ Page contentType = "text/html; charset = GBK" %> <% @ page import = "java. util. * "%> <% // The property set at this time can be obtained by any user. setAttribute ("name", "lone wolf"); // sets the attribute application. setAttribute ("date", new Date (); %>
applicationScopeDemo02

Example: applicationScopeDemo02.jsp

<%@ page contentType="text/html;charset=GBK"%><%@ page import="java.util.*"%><%    String refName = (String)application.getAttribute("name");    Date refDate = (Date)application.getAttribute("date");%>
Name: <% = refName %> Date: <% = refDate %>

Observe the running effect of the page:

  

Open multiple browser windows and runapplicationScopeDemo02.jspThe result is displayed. Because the attribute range is set on the server, any user connected to the server can obtain this attribute. Of course, if the server is disabled, this attribute will disappear.

For example, shut down the Tomcat server before restarting it, and open the browser window to runapplicationScopeDemo02.jspThe result is as follows:

  

NOTE: If too many application attributes are set on the server, the server performance will be affected. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxoNCBpZD0 = "35. Further Supplement to the pagecontext attribute range"> 3.5. Further Supplement to the pageContext attribute range

The four attribute ranges described previously are actually set through the pageContext attribute range. Open the description document of pageContext.

  

The PageContext class inherits the JspContext class and defines the setAttribute method in the JspContext class as follows:

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

This method has an integer variable of scope, which indicates the storage range of an attribute.

  

  PageContextClass inheritedJspContextSo the abstract setAttribute method is implemented in the PageContext class:

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

ThissetAttribute()If you do not writeintTypescopeParameter, the default value of this parameter isPAGE_SCOPE, ThensetAttribute()The method ispageAttribute range.intType parameter scope isREQUEST_SCOPE, ThensetAttribute()The request attribute range is set. Similarly, the scope parameter passed isSESSION_SCOPEAndAPPLICATION_SCOPEThe setAttribute () method is set to the session attribute range and application attribute range.

The following code observes the functions of these four attribute range constants. Take request as an example.

Example: pageScopeDemo04.jsp

<% @ Page contentType = "text/html; charset = GBK" %> <% @ page import = "java. util. * "%> <% pageContext. setAttribute ("name", "lone wolf", PageContext. REQUEST_SCOPE); // sets attributes and specifies the attribute range pageContext. setAttribute ("date", new Date (), PageContext. REQUEST_SCOPE); // sets the attribute and specifies the attribute range. %>
  

PageScopeDemo05.jsp

<% @ Page contentType = "text/html; charset = GBK" %> <% @ page import = "java. util. * "%> <% // use the request object to obtain the String refName = (String) request. getAttribute ("name"); Date refDate = (Date) request. getAttribute ("date"); // You can also use the pageContext object to obtain attributes. You only need to specify the attribute range of the object when obtaining the attributes. String refName2 = (String) pageContext. getAttribute ("name", PageContext. REQUEST_SCOPE); Date refDate2 = (Date) pageContext. getAttribute ("date", PageContext. REQUEST_SCOPE); %> Get attributes using the request object:
Name: <% = refName %> Date: <% = refDate %> Use the pageContext object to obtain attributes: Name: <% = refName2 %> Date: <% = refDate2 %>

Running result:

  

From the running result, you can see:pageScopeDemo04.jspYespageContextObject callsetAttribute()The attribute range of the method isrequestTherefore, when calling this method, set a scope range constant of the int typeREQUEST_SCOPEPassed in, thisREQUEST_SCOPEThe function of the attribute range constant is to tellpageContextThe property range to be set for the object is the property range of the request, sopageScopeDemo05.jspThis page can be used directlyrequest.getAttribute(); Method obtained inpageScopeDemo04.jspSet attributes.

4. Use Cases of four attributes of jsp 1. request: if the user sends a request to the server and the data generated is useless after reading the data, such data will exist in the request domain, news data is useless for users. 2. session, the user needs to view his/her shopping information and wait for a while before using the shopping data for checkout. 3. application (servletContext): If the customer sends a request to the server, the data generated will be used up and used up by the user. data such as this will exist in the application (servletContext) domain, like chat data.

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.