For ServletContextListener's contextDestroyed(), it is ok to get the attribute within this method through the event obj. Whereas in HttpSessionListener's sessionDestroyed(). it will throw an IllegalStateException when trying to obtain the attribute from a session. So does anyone know the rationale behind this inconsistency?
posted 14 years ago
According to the spec, sessionDestroyed(HttpSessionEvent se)Notification that a session was invalidated. This happens after session invalidation. When you tried to get a session attribute ref, you will get IllegalStateException. While in ServletContextListener, contextDestroyed(ServletContextEvent sce)Notification that the servlet context is about to be shut down. This happens before the invalidation, so reference to any context attributes is still valid.