Problems caused by bean scope changes when JSP uses get/setproperty

Source: Internet
Author: User

This article is a discussion post from the forum. I have already refined it.

Among them, huangwen9 on the 35 th floor and huangpeihp on the 37 th Floor provide correct explanations.

Original post here: http://topic.csdn.net/u/20080410/16/59a5734b-d4b5-490d-a4ab-9f63dc7a2708.html

System symptom
1. the bean to be tested is

package net.java2000.html; 

public class CounterBean ...{ 
  private int count = 0; 

  public CounterBean() ...{ 
  } 

  public int getCount() ...{ 
    return count; 
  } 

  public void setCount(int count) ...{ 
    this.count = count; 
  } 
}

2. the JSP for testing is

<jsp:useBean id="myBean" scope="session" class="net.java2000.html.CounterBean" />
<jsp:getProperty name="myBean" property="count" />
<br>
<%=myBean.getCount()%>
<jsp:setProperty name="myBean" property="count" value="<%=myBean.getCount()+1%>" />

3. The running result is
0
0
After refreshing
1
1
After refreshing
2
2
After refreshing
3
3

4 OK. If we change the session in the JSP file to Application

<jsp:useBean id="myBean" scope="application" class="net.java2000.html.CounterBean" />
<jsp:getProperty name="myBean" property="count" />
<br>
<%=myBean.getCount()%>
<jsp:setProperty name="myBean" property="count" value="<%=myBean.getCount()+1%>" />

5. The running result is
First refresh
4
0
Second refresh
1
0
Third refresh
1
0

It will be a number no matter how it is refreshed in the future

Is it a JSP bug?

System Analysis
1. Check the generated source code first.

 

Net.java2000.html. counterbean mybean = NULL;
Synchronized (Application )...{
// ######## Note: 1 ############
Mybean = (net.java2000.html. counterbean) _ jspx_page_context.getattribute ("mybean", pagecontext. application_scope );
If (mybean = NULL )...{
Mybean = new net.java2000.html. counterbean ();
_ Jspx_page_context.setattribute ("mybean", mybean, pagecontext. application_scope );
}
}
Out. Write ('');
Out. Write ('');
// ######## Note: here 2 ############

Counter. counterbean) _ jspx_page_context.findattribute ("mybean"). getcount ())));
Out. Write ("");
Out. Write ("
");
// ######## Note: here 3 ############
Out. Print (mybean. getcount ());
Out. Write ('');
Out. Write ('');

// ######## Note 4 ############

Org. Apache. Jasper. runtime. jspruntimelibrary. handlesetproperty (_ jspx_page_context.findattribute ("mybean"), "Count ",
Mybean. getcount () + 1 );

2. Let's take a look at the description of the findattribute method.

/***//**
* Searches for the named attribute in page, request, Session (if valid ),
* And application scope (s) in order and returns the value associated or
* Null.
*
* @ Param name the name of the attribute to search
* @ Return the value associated or null
* @ Throws nullpointerexception if the name is null
*/

Abstract Public object findattribute (string name );

3. The problem lies in the findattribute.

It is queried in the order of page, request, Session (if valid), and application. If the previous one is found, it will be returned.
1) Our program started as a session, so the number regularly rose from 0 to 3. At this time, the number in the session is 4.
2) When we modify the application, mybean becomes a new object and is saved in pagecontext. application_scope. The value is 0.
3) when the output to #2 is run, the findattri will first check the mybean with the same name in the session because the session has not expired, so the 4
4) #3 output the new object. The so-called number is 0.
5) #4 is a bit strange. He adds the value of the newly created mybean object to 1 and saves it to the mybean of the session. The number is 0 + 1 = 1;

4 refresh again
1) #1 there is nothing special. I got the object 0.
2) #2 because of the last refresh, the value is 1.
3) $3 is still 0
4) Repeat the previous refresh step again. The value 0 + 1 = 1 is then saved to the session.

Summary:
Due to the special nature of findattriute, it is very easy to restart the container when necessary, but an effective solution.
Just as if the operating system has a problem and cannot be solved, it is the same as restarting the machine by mistake. Haha!

Extension:
What if I changed to application after four refresh times and changed to session ??

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.