• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can't cast JspContext to PageContext ???

 
Vince Hon
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • 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
 
nils appeldoorn
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic