Program to demonstrate creation of a ServletContextListener using @WebListener annotation.
Output of the program :
package com.hubberspot.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
// @WebListener annotation informs container that
// this class is a web listener which will listen to
// various events happening during lifecycle of application
// Here this class listens to StartUp and ShutDown of the
// application.
// We make class implements ServletContextListener which has two
// methods contextInitialized() and contextDestroyed() , which
// are called by the container whenever a servlet context is
// started or shutdown
@WebListener
public class StartStopAppListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println("Servlet Context Initialized ... ");
}
public void contextDestroyed(ServletContextEvent servletContextEvent) {
System.out.println("Servlet Context Destroyed ... ");
}
}
Output of the program :
