Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

PageContext and jsp:useBean

 
Steven Colley
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks, could you help me here please?

We have this class:



What is the output for the items?

1 -



2 -




Tks.
 
shweta bulbule
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) RequestBean
2) RequestBean
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Rodrigo Alvarez
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
shweta bulbule
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


(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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, i m such a Fool , didn't pay attention to what is written..
thank you so much Narendra.

Shweta
 
Steven Colley
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic