• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Simple Tag - Get Request Object

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Sheriff
Posts: 67682
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67682
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67682
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for posting your solution for the problem Colin. Helped me solve my problem too.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67682
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67682
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes.
 
We can walk to school together. And we can both read this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic