what is the equivalence of contextDestroyed() in S

2019-07-03 22:37发布

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.

2条回答
放我归山
2楼-- · 2019-07-03 23:27

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 ###");
    }
}
查看更多
放我归山
3楼-- · 2019-07-03 23:30

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

servletContext.addListener(YourServletContextListener.class);
查看更多
登录 后发表回答