This J2EE tip demonstrates use of ServletContextListener. This event class handles notifications about changes to the servlet context of the Web application that they are part of. This can be explained as if someone is present on the server and dynamically informing us about the events that are occuring on the server. There acquire need of Listners. Therefore, ServletContextListner is helpful in informing about context Initilazation and destruction.

import javax.servlet.ServletContextListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.*;

public  class MyListener implements ServletContextListener {
  private ServletContext context = null;

  /*This method is invoked when the Web Application has been removed 
  and is no longer able to accept requests

  public void contextDestroyed(ServletContextEvent event)
    //Output a simple message to the server's console
    System.out.println("The Simple Web App. Has Been Removed");
    this.context = null;


  //This method is invoked when the Web Application
  //is ready to service requests

  public void contextInitialized(ServletContextEvent event)
    this.context = event.getServletContext();

    //Output a simple message to the server's console
    System.out.println("The Simple Web App. Is Ready");


Parent Category: Java EE Tips