Servlets give you more when it comes to this problem. And in the Java world, events are called listeners. There are several helpful listeners:
javax.servlet.ServletContextListener
void contextDestroyed(ServletContextEvent sce) Called when the servlet context should be destroyed.
void contextInitialized(ServletContextEvent sce) Called when the web application is ready to process requests.
javax.servlet.ServletContextAttributeListener
void attributeAdded(ServletContextAttributeEvent scae) Invoked when a new attribute is added to the servlet context.
void attributeRemoved(ServletContextAttributeEvent scae) Invoked when an attribute is removed from the servlet context.
void attributeReplaced(ServletContextAttributeEvent scae) Called when the servlet context attribute is replaced.
javax.servlet.http.HttpSessionListener
void sessionCreated(HttpSessionEvent se) Called when a session is created.
void sessionDestroyed(HttpSessionEvent se) Called when the session is invalid.
javax.servlet.http.HttpSessionAttributeListener
void attributeAdded(HttpSessionBindingEvent se) Called when an attribute is added to the session.
void attributeRemoved(HttpSessionBindingEvent se) Called when the attribute is removed from the session.
void attributeReplaced(HttpSessionBindingEvent se) Called when the attribute is replaced in the session.
source share