• 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

How to get reference to ServletContext in EventListeners.

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,
I have implemented HttpSessionBindingListener as i want to track when a particular user was bound or unbound to the session.
I have to store some values in ServletContext (context scope), but inside this class (which implements HttpSessionBindingListener) i am not able to get the reference to ServletContext.
Following is the code snippet of valueBound method of my class :-

Ideally the code in Bold should work and get the servlet context for me.
But it doesn't, coz HttpSession Interface (of servlet specification 2.2) doesn't define getServletContext() method (I am using Tomcat 3, which works with Servlet Specification 2.2).
So the method getServletContext() must have been included in the later specifications like 2.3 or 2.4
Is there anyway which could solve my requirement, which is to get the reference to the context of the current web application, as i need to store some values in the context scope.
TIA
[ May 05, 2004: Message edited by: Manish Sachdev ]
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You must first obtain reference to the ServletConfig could do: cntxRef=configRef.getServletContext and then cntx.getAttribute("attName")
you of course can also use to set the variable.
 
M Sharma
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the complete code :-


Here is the hierarchy of the classes :-

With this hierarchy, i wont be able to get the reference to ServletConfig also.
I think that, in case of servlets the container passes the ServletConfig reference through init(ServletConfig sc) method and also because the servlet indirectly implements the Servlet Interface, which has the method getServletConfig().
In my case, i can't find getServletConfig() method in the hierarchy which my class follows.
Please Help !!!
 
M Sharma
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this possible ???
Plzzz Help !!!
 
Sheriff
Posts: 67746
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
Servlet 2.2 is two revs out of date. Time to move on!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic