what is the equivalence of contextDestroyed() in ServletContainerInitializer?

随声附和 提交于 2019-12-10 19:01:50

问题


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.


回答1:


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

servletContext.addListener(YourServletContextListener.class);



回答2:


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 ###");
    }
}


来源:https://stackoverflow.com/questions/35724728/what-is-the-equivalence-of-contextdestroyed-in-servletcontainerinitializer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!