Java web application context listener and events example in servlet

This tutorial show you the example of application context listeners and events, as you know form last tutorials there are two life cycle and three attributes related listener and events are there in servlet context level. Here we will the example of both.

Listeners are added in servlet 2.3 versions, the classed and interfaces come with servlet API.

ServletContextListener Example

To note how many times your application is restarted, it may be for any reason, we can use this listener interface by implementing the same.

The ServletContextListener is an interface inside servlet API, whenever you started your server, it will also start application deployed in server one by one, and it has to create a ServletContext object for each application deployed. Creating ServletContext object is an event listener and it called the contextInitilized () method, the general term is starting web application.

The ServletContext object will be created only once in the application, whenever you stop the server it destroy the ServletContext object also, so destroying context is also an event to listener and it make a call to contexxtDestroyed() method.

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class ServletContextListenerExample implements ServletContextListener {
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("Application has started");
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("Application has stop");

So if you want to know the root cause of starting or stopping the application, you can use the ServletContextListener.

ServletContextAttributeListner Example

To note how many time the context attributes are added or removed or updated for an application, we can use this listener.

The ServletContextAttributeListner is an interface inside servlet API, it has three methods: public void attributeAdded(ServletContextAttributeEvent scab)

public void attributeRemoved(ServletContextAttributeEvent scab)
public void attributeReplaced(ServletContextAttributeEvent scab)

So whenever you give a call to any servlet and that servlet is adding or updating or removing any attributes form or to Servlet context object the correspondent method will be called.

Below is the full code of example of ServletContextAttributeListner.

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
public class ServletContextAttributeListnerExample implements ServletContextAttributeListener {
    public void attributeAdded(ServletContextAttributeEvent scab) {
        System.out.println("An attributes is added in Servlet context");
    public void attributeRemoved(ServletContextAttributeEvent scab) {
        System.out.println("An attributes is removed from Servlet context");
    public void attributeReplaced(ServletContextAttributeEvent scab) {
        System.out.println("An attributes is replaced in Servlet context");

Register listener in web.xml

After creating the listener classed you have to register same in web.xml file as listener. Below is the example of how the listener can be register in web.xml.


We cannot make any call to listener from browser. For that we have to make event for which the listener has been register inside web.xml.