Http Session listener and events example in servlet

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

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

HttpSessionListener Example

To note how many times your application users created the session or in simple world visited the application, it may be for any reason, we can use this listener interface by implementing the same.

The HttpSessionListener is an interface inside servlet API, whenever you want to log the user session startup or destroyed, you can use this listener.

This will call the sessionCreated() method when any user start the application or create the http session, and it will call the sessionDestroyed() method when user destroyed the session, or he explicitly destroyed the http session, in normal words he logged out from application. Some time user forgot to logout the application; in this case the session will be destroyed automatically when session has time out.

package com.java.connect.listener;
 
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
 
public class HttpSessionListenerExample implements HttpSessionListener {
 
    public void sessionCreated(HttpSessionEvent se) {
        System.out.println("An User session has been started");
    }
 
    public void sessionDestroyed(HttpSessionEvent se) {
        System.out.println("An User session has been destroyed");
    }
 
}

So if you want to know log the user session starting and ending, you can use the ServletContextListener.

HttpSessionAttributeListener Example

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

The HttpSessionAttributeListener is an interface inside servlet API, it has three methods:

public void attributeAdded(HttpSessionBindingEvent se);
 
public void attributeRemoved(HttpSessionBindingEvent se);
 
public void attributeReplaced(HttpSessionBindingEvent se);

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

Below is the full code of example of HttpSessionAttributeListener.

package com.java.connect.listener;
 
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
 
public class HttpSessionAttributeListenerExample implements HttpSessionAttributeListener {
 
    public void attributeAdded(HttpSessionBindingEvent se) {
        System.out.println("one attribute is added in User session");
    }
 
    public void attributeRemoved(HttpSessionBindingEvent se) {
        System.out.println("one attribute is removed form User session");
    }
 
    public void attributeReplaced(HttpSessionBindingEvent se) {
        System.out.println("one attribute is replaced in User session");
    }
 
}

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.

<listener>
    <listener-class>com.java.connect.listener.HttpSessionListenerExample</listener-class>
</listener>
<listener>
    <listener-class>com.java.connect.listener.HttpSessionAttributeListenerExample</listener-class>
</listener>

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