I have to create a class that implements ServletContextListener
to add an event during the initialization or the shutdown of Tomcat. However, the class has to be located in a jar file inside WEB-INF/lib
. After doing some readings, I found out that this is not possible, and the alternative is to use ServletContainerInitializer
. However, only onStartup()
method is available.
Is there any other alternatives where I can also add an event during the shutdown or destruction of the web application?
I am using Tomcat 8 and Java 8 btw.
Not sure how you tested your code. But this the ServletContextListener works fine for me on Tomcat 8.5.5. Just try this code, no need to put this to separate JAR file.
Let your
ServletContainerInitializer
programmatically add aServletContextListener
which in turn does the desired job in itscontextDestroyed()
.