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


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "PageContext and jsp:useBean" Watch "PageContext and jsp:useBean" New topic
Author

PageContext and jsp:useBean

Steven Colley
Ranch Hand

Joined: Feb 18, 2005
Posts: 290
Hi folks, could you help me here please?

We have this class:



What is the output for the items?

1 -



2 -




Tks.


SCJP | SCWCD | SCBCD | SCWSD 5 | SCEA (I) 1.4 | SCEA 5 | IBM SOA 669
shweta bulbule
Ranch Hand

Joined: Mar 24, 2006
Posts: 30
1) RequestBean
2) RequestBean


Thanks,<br />Shweta
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Hi,

In both codes the <jsp:useBean> define the bean attribute in the page scope. so when you use <jsp:getProperty> it will always get the bean in the page scope. The pageContext.setAttribute method define the attribute in the request scope.

so the output is alway the name value set by your no argument constructor. That is DefaultMyClass.

Thanks
[ April 13, 2006: Message edited by: Narendra Dhande ]

Narendra Dhande
SCJP 1.4,SCWCD 1.4, SCBCD 5.0, SCDJWS 5.0, SCEA 5.0
Rodrigo Alvarez
Ranch Hand

Joined: Apr 10, 2006
Posts: 75
This makes me wonder, what error would this generate:



(note that Bean is not a sub-class of MyClass)

=> I guess the second line would just replace the instance of MyClass with an instance of Bean (assuming that such class exists somewhere). But I guess the third line would still try to manipulate it as an instance of MyClass

=> compile-time error or even translation time error?


It is a mistake to think you can solve any major problems just with potatoes.<br />--Douglas Adams
shweta bulbule
Ranch Hand

Joined: Mar 24, 2006
Posts: 30
it may sound a silly question but

"The pageContext.setAttribute method define the attribute in the request scope."

how come?? doesn't it set an attribute at page scope?
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950


(note that Bean is not a sub-class of MyClass)

=> I guess the second line would just replace the instance of MyClass with an instance of Bean (assuming that such class exists somewhere). But I guess the third line would still try to manipulate it as an instance of MyClass

=> compile-time error or even translation time error?[/qb]<hr></blockquote>

You are right. I had tested it. The getProperty cast the object as defined in the useBean. It is run-time error. The Container throw ClassCastException. But the request attribute change. It you use ${bean.name} there is no error. and it will display RequestBean.

Hi Sweta,

You can define attributes to any 4 scope useing pageContext object. There is overloded methods for this in pageComtext object. The third argument in the setAttribute method is the scope.

Hope this help

Thanks
[ April 13, 2006: Message edited by: Narendra Dhande ]
shweta bulbule
Ranch Hand

Joined: Mar 24, 2006
Posts: 30
Oh, i m such a Fool , didn't pay attention to what is written..
thank you so much Narendra.

Shweta
Steven Colley
Ranch Hand

Joined: Feb 18, 2005
Posts: 290
Hi Simon and Narendra,

Tks for notincing and discussing the probable exception regarding those different kinf of class (Bean and MyClass).

But it was a typo.

It really should be "MyClass" instance. Sorry!

Tks for all.
 
 
subject: PageContext and jsp:useBean