• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

search order for attribute in scopes in new session

 
Raoul Kalk
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's another useBean/setProperty problem that I cannot really explain.

In as jsp I have a scriptlet where I create an object (aBean) and add it to the request. The bean has a property name. Ofcours, no one should ever use scriplets, we've all learned that ;-)

request.setAttribute("attrbean",aBean);

To check, I have ${attrbean.name} right after the scriplet part and it shows the correct value when the object was created.

Now the jsp continues with a usebean.

jsp:useBean id="attrbean" type="myBean" class="myBean" scope="session"
jsp:setProperty name="attrbean" property="name" value="changed name"
/jspUsebean


This would look for an attribute name "attrbean" in the session scope and if not found, then create one and set the name property (or doest it look in all scopes and if not found create one in the scope given in the scope parameter? not sure here.)

Next in jsp, the name attribute of the bean is shown again: ${attrbean.name}

One would assume that the scope search order for an attribute would be Page, Request, Session, Application. So it should still find the initial value for the name property of the object that was added to the request.

In reality it shows the value "changed name" only the first time, so when the session is created. After that, on each reload it shows the initial value from the bean on the request. To see if a session dies, I'm using a session listener. And on every new session the behavior is exactly as described.

It's obvious that the useBean creates a new object for a new session, but the ${attrbean.name} should never retrieve it from the session but from the request.

Anyone any idea? Could it be that because I have everything in one jsp, the container does not yet see that request attribute because it's all in a fraction of a second? But a new reload does see the atttribute? Thanks for your help in advance.
 
Raoul Kalk
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by kees kalknagel:
Here's another useBean/setProperty problem that I cannot really explain.
jsp:useBean id="attrbean" type="myBean" class="myBean" scope="session"
jsp:setProperty name="attrbean" property="name" value="changed name"
/jspUsebean



As I see it now, the scope option tells useBean to look in that scope and in that scope alone. If it can't find the attribute there, it will create one in that same scope. So in my example it should never be retrieved as there is already an equally named attribute in the request scope. If the scope parameter is not provided, then it will work in the page scope. Correct me if I'm wrong here.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct me if I'm wrong here.

You're right.
 
Raoul Kalk
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Christophe Verre:

You're right.


Ok, why would the jsp ever get the attribute from the session when it should find the equally named attribute in the request scope as well? What am I missing here?
 
Justin Rundle
Ranch Hand
Posts: 123
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While on the subject about <jsp:useBean> if the scope isn't defined the default is page and the container will search for the bound attribute starting in page, then request, then session and lastly application, yes/no?
 
Raoul Kalk
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Justin Rundle:
While on the subject about <jsp:useBean> if the scope isn't defined the default is page and the container will search for the bound attribute starting in page, then request, then session and lastly application, yes/no?


Correct, that's why I am wondering why my example works the way it does. It somehow gets the session attribute (once, on a new session), even though there is an equally named attribute on the request.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Raoul",
Welcome to the ranch. You may not be aware of the ranch Naming Policy. Please read it carefully and change your name accordingly (you need to set both first and last names). Thank you.
 
Raoul Kalk
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Christophe Verre:
"Raoul",
Welcome to the ranch. You may not be aware of the ranch Naming Policy. Please read it carefully and change your name accordingly (you need to set both first and last names). Thank you.


Ok, sorry for that, profile updated. Raoul is my real name though :-)
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you
 
Ashu Jain
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am also confused why it is printing value of attribute from session scope. It should be printing from request scope at all times as the order of search is Page, request, session and application.

Can anyone please explain.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic