wellnwill

Servlet

There are various task that are performed by web container and developer during web application execution cycle. Some of these task binds with corresponding events. There are listeners provided for each type of event. When task is performed, event is triggered, listener listens to these event and react to those events All Listener interfaces and even classes are part of javax.servlet package.

How to write listeners

Any class which Implements any Listener interface of javax.servlet package and Annotated with @WebListener is a Listener.

Alternatively to the annotation listener can be configured in web.xml. All methods of Listener interface must be overridden. Container initialized by container at start up.

Configure in web.xml

<listener>
   <listener-class>com.atuldwivedi.learnservlet.listener.ContextListener</listener-class>
</listener>

Configure using annotation

@WebListener
public class ContextListener implements ServletContextListener {
...
}

Example

  • An example of writing Listener class for ServletContextListener
  • ServletContextListener has two methods
    • public void contextInitialized(ServletContextEvent sce)
    • public void contextDestroyed(ServletContextEvent sce)
  • Both methods need to be overridden in user’s Listener class
  • Below is the code for the same
package com.atuldwivedi.learnservlet.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
 * Application Lifecycle Listener implementation class ContextListener
 * 
 */
@WebListener
public class ContextListener implements ServletContextListener {

  /**
  * Default constructor.
  */
 public ContextListener() {
  // TODO Auto-generated constructor stub
 }

 /**
  * @see ServletContextListener#contextDestroyed(ServletContextEvent)
  */
 public void contextDestroyed(ServletContextEvent arg0) {
  ServletContext ctx = arg0.getServletContext();
  System.out.println(this.getClass().getName() + " contextDestroyed "
    + ctx);
 }

 /**
  * @see ServletContextListener#contextInitialized(ServletContextEvent)
  */
 public void contextInitialized(ServletContextEvent arg0) {
  ServletContext ctx = arg0.getServletContext();
  System.out.println(this.getClass().getName() + " contextInitialized "
    + ctx);
 }

}

Internal working

Container startup

  • Container create ServletContext object
  • ServletContext sctx = new ServletContextImpl();
  • Initializes sctx with context parameters configured in web.xml
  • Container creates ServletContextEvent object
  • ServletContextEvent sce = new ServletContextEvent(sctx);
  • Container creates ServletContextListener object
  • ServletContextListener listener = new ServletContextListenerImpl();
  • Container invokes contexInitialized() method of ServletContextListener
  • listener.contexInitialized(sce);

Container shut down

  • Container invokes contextDestryed() method of ServletContextListener
  • listener.contextDestroyed(sce);
  • Destroyed the ServletContext object
 
Footer with Map