what is the equivalence of contextDestroyed() in ServletContainerInitializer?
Asked Answered
P

2

3

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.

Parthen answered 1/3, 2016 at 13:51 Comment(0)
G
5

Let your ServletContainerInitializer programmatically add a ServletContextListener which in turn does the desired job in its contextDestroyed().

servletContext.addListener(YourServletContextListener.class);
Gardie answered 3/10, 2016 at 7:16 Comment(0)
F
-1

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.

@WebListener
public class AppContextListener implements ServletContextListener{

    Logger log = LoggerFactory.getLogger(AppContextListener.class);

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {

    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        log.info("### Context is destroyed ###");
    }
}
Foolscap answered 3/10, 2016 at 5:32 Comment(3)
Actually I though r051cky can't use the separate jar to include the ServletContainerInitializer implementation. The configuration in web.xml could be used. <listener> <listener-class> com.example.AppContextListener </listener-class> </listener>Foolscap
No need to argue with you. I prefer to know what my application do.Foolscap
To be honest currently working on Tapestry projects, which is the bunch of magic. But you are true, the Spring especially Spring Boot uses all of these too. And you are right that is very, very comfortable to use. But if you are going to do anything very custom, you are in big pain and headache sometimes. :)Foolscap

© 2022 - 2024 — McMap. All rights reserved.