You can try to catch the JVM shutdown event as follows:
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
System.out.println("BYE BYE");
}
});
Another option is to implement ServletContextListener using @WebListener Annotation. In this case, no xml configuration is required.
@WebListener
public class MyLifeCycleListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent event) {
}
public void contextDestroyed(ServletContextEvent event) {
}
}
source
share