On Fri, 11 May 2001, Matthew Fowkes wrote:

> I was wondering if anyone else has tried to access the ServletContext from
> the session object.  I have written a class that implements
> HttpSessionBindingListener so I can detect when a users session expires.
> When the session expires, the valueUnbound method is called and is passed an
> event.  From that event I can retrieve the users session object and this is
> where the problem begins.  The only object I have is the users session
> object, but I need to somehow get an attribute from the ServletContext to
> mark that the user session has expired.  Has anyone on the list done this
> before?
> 
> Thanks in advance.
> 
> Matt
> 

In a Servlet 2.3 container (like Tomcat 4.0), you can do this pretty
easily -- a getServletContext() method was recently added to the
HttpSession API for exactly this sort of thing.

In a Servlet 2.2 environment, however, the easiest way is probably to
store a reference to the servlet context in your listener object when it's
first created (i.e. before you add it as a session attribute).  That way,
it can reference the servlet context object in the valueBound() and
valueUnbound() methods.

Craig McClanahan


Reply via email to