×
☰ See All Chapters

Event and listener in servlet

An event is a change in the state in a life cycle of an object. With respect to ServletContext object, when the ServletContext object is created and destroyed, or when an attribute (a key/value pair) is created, removed, or replaced, we can say these moments as events which we can listen to. We can take advantage of the listeners which listen to these important stages in the life cycle. For example, since ServletContext object is created by the servlet container when it initializes, we can therefore utilize this event, such as loading a JDBC driver or creating a database connection object. Or, load a file and save it in a variable in the ServletContext object so that it is accessible from all the servlets in the web application. Similarly for events that get fired when the ServletContext is destroyed, you can write code that does some cleanup operation, such as closing files or closing a database connection. Below table list some of the servlet API provided event and listeners:

Event Classes

Listener Interfaces

ServletContextEvent

ServletContextListener

ServletContextAttributeEvent

ServletContextAttributeListener

ServletRequestEvent

ServletRequestListener

ServletRequestAttributeEvent

ServletRequestAttributeListener

HttpSessionEvent

HttpSessionListener

The ServletContextListener Interface

ServletContextListener interface to listen to the ServletContext life cycle events. Its signature is given as follows:

public interface ServletContextListener extends java.util.EventListener

The ServletContextListener interface has two methods: contextInitialized and contextDestroyed. The signatures for these methods are the following:

public void contextInitialized(ServletContextEvent sce):It is called when the servlet is ready to service requests

public void contextDestroyed(ServletContextEvent sce): It is called just before servlet context shut down

Your listener class must implement this interface to listen to ServletContext  life cycle events.

import javax.servlet.ServletContextListener;

import javax.servlet.ServletContextEvent;

 

public class MyListenerClass implements ServletContextListener {

 

        public void contextInitialized(ServletContextEvent cse) {

                System.out.println("Application initialized");

        }

 

        public void contextDestroyed(ServletContextEvent cse) {

                System.out.println("Application shut down");

        }

}

contextInitialized method is similar to writing code in a servlet's init( ) method, and the contextDestroyed method has a similar effect as a servlet's destroy( ) method. However, using application events make the codes available throughout the whole application, not only from inside a servlet.

The ServletContextEvent Class

contextInitialized()  and contextDestroyed() methods of  ServletContextListener interface  has ServletContextEvent class as the parameter. This class has the following signature:

public class ServletContextEvent extends java.util.EventObject

The ServletContextEvent has only one method: getServletContext. This method returns the updated ServletContext.

Configuring listeners in deployment descriptor (web.xml)

The <listener> element must come before the <servlet> part.

<web-app>

        <listener>

                <listener-class>MyListenerClass</listener-class>

        </listener>

        <servlet>

                <servlet-name>hello</servlet-name>

                <servlet-class>MyServletClass</servlet-class>

        </servlet>

        <servlet-mapping>

                <servlet-name>hello</servlet-name>

                <url-pattern>/helli</url-pattern>

        </servlet-mapping>

</web-app>

 

You can have more than one listener class. In order to have more than one listener we have to configure all the listener classes in the deployment descriptor, as follows:

<web-app>

        <listener>

                <listener-class>MyListenerClassOne</listener-class>

        </listener>

        <listener>

                <listener-class>MyListenerClassTwo</listener-class>

        </listener>

        <servlet>

                <servlet-name>hello</servlet-name>

                <servlet-class>MyServletClass</servlet-class>

        </servlet>

        <servlet-mapping>

                <servlet-name>hello</servlet-name>

                <url-pattern>/helli</url-pattern>

        </servlet-mapping>

</web-app>

 


All Chapters
Author