Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Simple Tag - Get Request Object  RSS feed

 
Colin Shine
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I'm trying to come up with a workaround for the lack of any method in the SimpleTagSupport API for getting the current Servlet Request. I read a hint on a website that the EL expression evaluation API can be used, and I'm trying to get it to work using a variation of the following approach. Is what I'm trying even possible?

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a workaround for the lack of any method in the SimpleTagSupport API


No workaround is necessary.

Did you check the API of PageContext? (Hint: the JspContext available to the tag is an instance of PageContext).
 
Colin Shine
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've tried the following:



but the compiler complains, saying I can't cast from a JspContext to a PageContext.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler? What's the nature of the error?

From the API docs:

javax.servlet.jsp
Class JspContext

java.lang.Object
javax.servlet.jsp.JspContext
Direct Known Subclasses:
PageContext


See http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/jsp/JspContext.html
 
Colin Shine
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error I'm getting from the compiler is:
"Cannot cast from JspContext to PageContext"

I'm totally stumped, I've checked out the javadocs on the sun website at http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/jsp/PageContext.html which say exactly what you say, but yet my compiler doesn't allow it.
 
Colin Shine
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear,

I've solved it! There was a servlet-2.3.jar and a jsp-api-2.0.jar file on my project classpath, and the compiler was loading the PageContext from the servlet jar, which seems to be incompatible with the jsp-api JspContext. Thanks for your help.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah yes! That would do it. Make sure that you don't have any out-of-date API in the classpath!
 
suneel gundlapalli
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for posting your solution for the problem Colin. Helped me solve my problem too.
 
Jeff Schramm
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
suneel gundlapalli wrote:Thanks for posting your solution for the problem Colin. Helped me solve my problem too.

mine, too
 
Vaib Gupta
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I have one basic doubt. How did this approach work? PageContext is a child of JspContext. And PageContext has extra methods such as getRequest() which are not defined for JspContext. If I cast an object of JspContext into PageContext and access getRequest from this instance, shouldn't I get a run time exception? How can the object of JspContext access a method which it has not defined? Is this some kind of a new trick?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, this is just basic Java. The instance of the object is an instance of PageContext to begin with, so there's no problem casting it to what it really is.
 
Vaib Gupta
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok...So you mean to say that the object returned has a reference type of JspContext but is actually an instance of PageContext...
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!