aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes jsp:useBean question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "jsp:useBean question" Watch "jsp:useBean question" New topic
Author

jsp:useBean question

david lee lee ck
Greenhorn

Joined: Jun 25, 2007
Posts: 4
Given the following 2 jsp files and bean class correctly defined:






Why is the output "FirstValue"?

Feeling quite puzzled over this....


SCJP 1.5, SCWCD 1.4
Raj Kumar
Ranch Hand

Joined: Sep 19, 2005
Posts: 65
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

Joined: Sep 19, 2005
Posts: 65
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

Joined: Jun 25, 2007
Posts: 4
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

Joined: Oct 21, 2005
Posts: 243
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

Joined: Jun 05, 2006
Posts: 254
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


SCJP,SCWCD, Into ATG now!
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707

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.


cmbhatt
D.R.Vinayak
Greenhorn

Joined: Mar 27, 2007
Posts: 18
<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


SCJP 95%<br />SCWCD(waiting for the big)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: jsp:useBean question
 
Similar Threads
JSP - UseBean
jsp:include
one more Jpilot ques.
Java Beans
Charles can you please clarify this question