In this article, we will see how to use event listeners in Java web programming.
With event listener, we can create our custom event driven code executions in web application.
ServletContextListener is the interface that gets informed about ServletContext lifecycle changes.
Lets see it in an example:
public class AppListener implements ServletContextListener
{
public void contextInitialized(ServletContextEvent sce)
{
sce.getServletContext().log("promometheus metric div 3 relay..");
}
public void contextDestroyed(ServletContextEvent sce)
{
sce.getServletContext().log("promometheus metric div 3..");
}
}
Lets see it in a java example.
In above example, we created our AppListener class and inherit it from ServletContextListener. We log some message for test our event listener work.