I would suggest looking at the chapter "Application Life Cycle Events" from the Servlet specification.
Depending on which version you are using, here are the relevant chapters and links to documents:
Listeners are used to notify about events in web applications, including state changes of ServletContext , HttpSession and ServletRequest . By implementing the predefined listener interfaces ( javax.servlet.ServletContextListener , javax.servlet.http.HttpSessionListener , javax.servlet.ServletRequestListener , etc.), the servlet container will notify you of some events that occur in your application. They have many potential features, such as performing one-time installation and shutdown tasks for applications, intercepting requests for logging, tracking HTTP session usage, etc.
shelley
source share