Listeners and web application events

In this post, you will learn:

  • What is a listener?
  • What is a web application event?
  • How web application events are processed?

What is a listener?

A listener is an optional component of a Java web application which facilitate event processing. An event represents a change of application state e.g. when an application is deployed its state is changed from undeployed to deployed. Performing some operations when an event occurs, is called event processing e.g Let there be a web application that loads some data form the database when it is deployed and saves it back when it is undeployed. Such loading and unloading can’t be performed from a servlet because it is not request processing.

How web application events are managed in a web application?

In order to manage web application events, listeners are defined for the events. These listeners are registered with the server to receive the notification of the events. When the event occurs the server notifies the registered listeners by invoking their callback methods.

Commonly used web application events & their listeners

1. ServletContextEvent – This event is generated when an application is deployed or undeployed. Implementation of ServletContextListener interface need to be provided to manage this event in an application. ServletContextListener interface provides following methods.

contextInitialized(): – this method is invoked by the server just after the application is deployed. It has following syntax:

public void contextInitialized(ServletContextEvent e);

contextDestroyed(): – this method is invoked by the server just before the application is undeployed. It has following syntax:

public void contextDestroyed(ServletContextEvent e);

In both these methods, an object of type ServletContextEvent is provided as argument. This object contains the reference of the ServletContext which is created or is being destroyed. ServletContextEvent class provides following method:

getServletContext(): – this method is used by the listeners to obtain the reference of the ServletContext. It has following syntax:

public ServletContext getServletcontext();

If an application provides the implementation of ServletContextListener then following sequence of operations are performed by the server when it is deployed:

servletcontextListener

Description of the diagram:

1.0 Application having ServletContextListener is deployed on the server.

1.1 ServletContext object is created for the application by the server.

1.2 ServletContextListener object is created by the server.

1.3 ServletContextEvent object is created by the server and reference of the ServletContext object is provided to it.

1.4 Notification of the application deployment is provided by the server to the ServletContextListener by invoking its contextIntialized() method. Reference of the ServletContextEvent object is passed as argument in this method so that the listener can obtain ServletContext through it.

2. ServletContextAttributeEvent – This event is generated when attributes are added, removed or replaced from ServletContext. Implementation of ServletContextAttributeListener interface need to be provided to manage this event in an application. ServletContextAttributeListener interface provides following methods.

attributeAdded(): – This method is invoked by the server just after an attribute is added to the current ServletContext. It has following syntax:

public void attributeAdded(ServletContextAttributeEvent e);

attributeRemoved(): – This method is invoked by the server just after an attribute is removed from the current ServletContext. It has following syntax:

public void attributeRemoved(ServletContextAttributeEvent e);

attributeReplaced(): – This method is invoked by the server just after an attribute is replaced int the current ServletContext. It has following syntax:

public void attributeReplaced(ServletContextAttributeEvent e);

In all these methods, an object of type ServletContextAttributeEvent is provided as argument. This object contains the name and value of the attribute which is added, removed or replaced. ServletContextAttributeEvent class provides following method:

getName(): – This method is used by the listener to obtain the name of the attribute which is added, removed or replaced. It has following syntax:

public String getName();

getValue(): – This method is used by the listener to obtain the value of the attribute. If the attribute is added, it returns the value of the attribute. If the attribute is removed, it returns the value of the removed attribute. If the attribute is replaced, it returns the old value of the attribute. It has following syntax:

public String getValue();

3. HttpSessionEvent – This event is generated when a session is created or destroyed. Implementation of HttpSessionListener interface need to be provided to manage this event in an application. HttpSessionListener interface provides following methods.

sessionCreated(): – this method is invoked by the server just after a HttpSession object is created. It has following syntax:

public void sessionCreated(HttpSessionEvent e);

sessionDestroyed(): – this method is invoked by the server when a session is invalidated or times out. It has following syntax:

public void sessionDestroyed(HttpSessionEvent e);

In both these methods, an object of type HttpSessionEvent is provided as argument. This object contains the reference of the HttpSession which is created or destroyed. HttpSessionEvent class provides following method:

getSession(): – this method is used by the listeners to obtain the reference of the bounded session. It has following syntax:

public HttpSession getSession();


Its time to recap the main concepts:

  1. In addition to servlets a web application can have listeners as well.
  2. Servlets are used for request processing and listeners are used for event processing.
  3. Listener objects are created by the server at the time of deployment before any servlet is created and initialized.
If you like the post, then share it...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

One thought on “Listeners and web application events

  1. a very good initiation neeraj sir……. also plz upload some of PDF books or part of the book which student can refer……..
    thnx harender

Leave a Reply

Your email address will not be published. Required fields are marked *