I am developing a Java web-application. The application connects to a Lucene index. I create a singleton instance of IndexSearcher. This instance opens some files. When I re
If you implement javax.servlet.ServletContextListener
and register that class in web.xml
as a <listener>
, then then contextDestroyed()
method will be called before the context is unloaded.
Implement a ServletContextListener.
@WebListener
public class LuceneConfig implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent event) {
// Do your job here during webapp startup.
}
@Override
public void contextDestroyed(ServletContextEvent event) {
// Do your job here during webapp shutdown.
}
}
If you're not on Servlet 3.0 yet (which is already out for 2 years though), then you need to remove the @WebListener
annotation and register it manually in web.xml
as follows:
<listener>
<listener-class>com.example.LuceneConfig</listener-class>
</listener>