This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Originally posted by Seenikkannan krishnasamy: request.setAttribute("person",per2); request.getSession().setAttribute("person",per3);
are you trying with same name "person"..isn't overwritten?
No, because one is set in the request scope and the other in the session scope: the code snipped works on two different attributes.
Originally posted by Seenikkannan krishnasamy: i think useBean getProperty work like Pagecontext.findAttribute()..searches for the named attribute in page,request,session and application in order returns value associated or null;
I think you are partially right. jsp:useBean works as you described if no scope is specified, but in Tridib's case the jsp code is explicitly specifying scope="session".
I fail to see where the problem is. I'll try it myself once I get home.
try specifying a different name. for example in your jsp:useBean tag, use an id of "sessionPerson" and then make the name in your jsp:getProperty tag match that.
I think what is happening is this getProperty tag is looking in the request scope first for an object named "person" and is finding the one you added in the servlet. the getProperty tag then stops instead of looking for the "person" bean you defined with jsp:useBean in the session scope. The bottom line is that jsp:getProperty does not NEED the jsp:useBean because the named reference exists already and will be available in the fully compiled servlet.
Originally posted by Clifton Eaton: The bottom line is that jsp:getProperty does not NEED the jsp:useBean because the named reference exists already and will be available in the fully compiled servlet.
It doesn't work either, because the jsp translates to:
which results in the following exception: org.apache.jasper.JasperException: Attempted a bean operation on a null object
And finally, the original jsp translates to:
which outputs "Request" due to the fact that getProperty uses pageContext.findAttribute("person") to get the bean.
I hope this helps somebody else as it did help me understand. [ March 30, 2007: Message edited by: Sergio Tridente ]
Joined: Apr 28, 2004
Thanks everybody for your great replies!!
The value of the name attribute in jsp:setProperty and jsp:getProperty will refer to an object that is obtained from the pageContext object through its findAttribute method.
Now, I understand why the output is so. But, what is the purpose of "scope" attribute in <jsp:useBean> action, if it can't make the Object available from correct scope?
The problem here is not with useBean, but with getProperty (it's not a problem actually). useBean correctly uses the "person" which is in the session. getProperty finds the "person" in the request first, because the scope is not being set.
The best would be to avoid setting attributes with same names in different scopes, to avoid hair-pulling bugs.
<jsp:useBean id="person" type="com.tridib.vo.Person" scope="session" /> creates an instance of the bean if it dosent exist, or uses the bean if it exists, The scope attribute is used to store this bean i.e person bean. when we use <jsp:getProperty property="name" name="person"/> we simply are trying to get the name property of person bean which is set previously i.e: using this:
Person per2 = new Person("Request"); Person per3 = new Person("Session"); request.setAttribute("person", per2);...Storing in request request.getSession().setAttribute("person", per3);...Storing in session
The getProperty will first look for the property in page , request , session and application scope and it returns value it gets first : in this case its request scope and hence the Request is returned to the output.