• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get reference to ServletContext in EventListeners.

 
M Sharma
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • 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 ]
 
J Craig
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Is this possible ???
Plzzz Help !!!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65340
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlet 2.2 is two revs out of date. Time to move on!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic