• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can't cast JspContext to PageContext ???

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In HFB p.547:


Remember that while Simple tags get a reference to a JspContext and Classic tags get a reference to a PageContext, the Simple tag's JspContext is usually a PageContext instance. So if your Simple tag handler needs access to PageContext-specific methods or fields, you'll have to cast it form a JspContext to the PageContext it really is on the heap



However, when I cast JspContext to PageContext in Simple tag, compile error occurs:



Can someone explain this ?

Thanks
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
see the J2EE API from Sun ...

java.lang.Object
- extended by javax.servlet.jsp.JspContext
- extended by javax.servlet.jsp.PageContext

you can see that PageContext extends JspContext, not the other way around, what your code assumes
 
Vince Hon
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks

But I still not clear, what's the code to do this:

"So if your Simple tag handler needs access to PageContext-specific methods or fields, you'll have to cast it form a JspContext to the PageContext "

??
 
nils appeldoorn
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you check the J2EE Tutorial http://java.sun.com/j2ee/1.4/docs/tutorial/doc, you'll see an example (http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JSPTags7.html) where they do the same in the doTag() method as you tried. the getJspContext() returns in the case of a SimpleTag a JspContext-object, that in fact is a PageContext-object

so, as I now wonder, does your Tag-class extend SimpleTagSupport?
 
Vince Hon
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Many thanks, nils, I realize that:


getJspContext() returns in the case of a SimpleTag a JspContext-object, that in fact is a PageContext-object



I want to do a cast because I want to GET A "REQUEST" OBJECT.
Since getRequest() is only defined in PageContext but not in JspContext.
(I realize JspContext can get attributes in any scope)

JspContext jspCtx;
PageContext pageCtx;

Only pageCtx.getRequest() is work even jspCtx and pageCtx referred to the same object.

In addition, could you explain how to code to do this ?


So if your Simple tag handler needs access to PageContext-specific methods or fields, you'll have to cast it form a JspContext to the PageContext

 
nils appeldoorn
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you already did that perfectly well! look at the example in the tutorial ... it's the same as your code!



that's all!

or don't we understand each other properly? now I'm in doubt ...

[ June 13, 2005: Message edited by: nils appeldoorn ]
[ June 13, 2005: Message edited by: nils appeldoorn ]
 
Sunglasses. AKA Coolness prosthetic. This tiny ad doesn't need shades:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic