Search code examples
guiceguice-servlet

Clear a session scoped object in Guice Servlet


Currently I have a session scoped object called MySessionData. Originally when logging out I just called

getThreadLocalRequest().getSession().invalidate();

The problem is now we have multiple session scoped objects and logging out only really requires the ony (MySessionData) be cleared. Does anybody know of a way to clear a single Session scoped object in Guice. If this were using traditional session objects I would just set the key value pair to null in the session map.


Solution

  • I usually use guice session objects as Holders. If you implements MySessionData like that, you just have to define a clear() method on it to remove or clear all the holded objects.

    If it is not enough, you can still invalidate the session. If you need to keep a reference on the Session for this invalidation, you can use a ThreadLocal variable and set it using a ServletFilter.