Execute code on webapp startup and shutdown using ServletContextListener
Sometimes you are in need of invoking some code during startup and shutdown of your web application. There are several ways to achieve this, but in this post I'm going to take a look at the ServletContextListener
.
How it works
ServletContextListener
is an interface that gets notified about ServletContext lifecycle changes. It offers two methods.
contextInitialized
- Is triggered when the web application is starting the initialization. This will be invoked before any of the filters and servlets are initialized.contextDestroyed
- Is triggered when the ServletContext is about to be destroyed. This will be invoked after all the servlets and filters have been destroyed.
Let's see how the ServletContextListener
implementation could look.
Read full article from Execute code on webapp startup and shutdown using ServletContextListener
No comments:
Post a Comment