• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

jsp:useBean question

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
please buy this thing and then I get a fat cut of the action:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic