Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jsp:useBean question

 
david lee lee ck
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given the following 2 jsp files and bean class correctly defined:






Why is the output "FirstValue"?

Feeling quite puzzled over this....
 
Raj Kumar
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, fist.jsp while defining the useBean you have mentioned the scope as session. So the attribute is set at the scope level.

If you could have opted the same thing (i.e) by mentioning the scope as session then you would have got the result as "SecondValue" since you have not done that the second bean have taken the default as "Page" scope and the call for include action is made dynamic you can't see the "secondValue" instead you see "FirstValue".

Even i'm a beginner let me know if anything is wrong.

thanks,
 
Raj Kumar
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pls. replace this:
(i.e) by mentioning the scope as session

with
(i.e) by mentioning the scope as session in the usebean of second.jsp

Sorry for the mistake
 
david lee lee ck
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the reply.

i applied different scope values to second.jsp and these are the results:

<jsp:useBean id="myBean" class="bean.MyBean" scope="page"/> return me "FirstValue"

<jsp:useBean id="myBean" class="bean.MyBean" scope="session"/>
<jsp:useBean id="myBean" class="bean.MyBean" scope="request"/>
<jsp:useBean id="myBean" class="bean.MyBean" scope="application"/> all return me "SecondValue"

so it seems that if use a scope lesser than the calling page's bean scope, then all changes made in second.jsp are lost when it returned back to first.jsp?

The puzzling thing to me is why is changes to the bean property lost? Is'nt both jsp pages working only on 1 bean.MyBean object?
 
Renu Radhika
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I feel it should return secondvalue only if the scope of bean in second jsp is session otherwise it should return firstvalue as that's the value of myBean set in session.Please share your thoughts on this?
 
Joshua Antony
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Me too, I think the output will be FirstValue
But I feel it should return secondvalue only if the scope of bean in second jsp is session

If the scope of the bean in second.jap is request then also I feel secondvalue will be returned

Regards,
Joshua
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If the scope of the bean in second.jap is request then also I feel secondvalue will be returned


I agree!

findAttribute() will search in PageContext, ServletRequest, HttpSession,
ServletContext order.
 
D.R.Vinayak
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<jsp:useBean id="myBean" class="bean.MyBean" scope="page"/> return me "FirstValue"


in the background it creates a pageContext object i.e. jsp pagecontext
when we call <jsp:getProperty > it uses findAttribute() method to get the value.It first looks in page , request,session the application in that order
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic