shutdown hook for java web application

user121196 picture user121196 · Oct 11, 2009 · Viewed 44.3k times · Source

I need to save some data preferrably when the java web application is stopped, or when tomcat is stopped. how can this be done? Edit: any drawback if I use the jvm shutdown hook?

Answer

leonm picture leonm · Oct 11, 2009

Use a class that implements ServletContextListener in your web.xml:

<web-app>
    <!-- Usual stuff here -->
    <listener>
        <listener-class>com.mycompany.MyClass</listener-class>
    </listener>
</web-app>