public void contextDestroyed(ServletContextEvent cse) { // write java code here that you want to run after the context is destroyed. }}

After you have done it. Configure you Listener in web.xml like this:

mypackage.MyListener

Please note that, the code written in Listener classes run even before the run-on-startup Servlets. And if need to write multiple Listener. Make sure you put then in right order. The Listeners are executed in the order they are configured.