aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Can't cast JspContext to PageContext ??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Can Watch "Can New topic
Author

Can't cast JspContext to PageContext ???

Vince Hon
Ranch Hand

Joined: Feb 11, 2003
Posts: 117
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


Vince Hon<br /> <br />SCJP 1.4 | SCWCD | SCBCD <br /><a href="http://vincehon.homeip.net:8000/VJW" target="_blank" rel="nofollow">http://vincehon.homeip.net:8000/VJW</a>
nils appeldoorn
Greenhorn

Joined: May 09, 2005
Posts: 16
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

Joined: Feb 11, 2003
Posts: 117
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

Joined: May 09, 2005
Posts: 16
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

Joined: Feb 11, 2003
Posts: 117
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

Joined: May 09, 2005
Posts: 16
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Can't cast JspContext to PageContext ???