HttpSessionListener Example

HttpSessionListener Example explains about How to use HttpSessionListener in a web application

What is HttpSessionListener?

For knowing about HttpSessionListener, First we must understand session. Http protocol is a "stateless" protocol. The word stateless means Http protocol can't persist the information in between client and server. When a client sends a request for any resource, server receive the request and process the request and returns response.  After returning the response the server terminates the connection. When a client forwards a request for a particular resource, server consider each and every request as a separate request. In order to avoid this burden we use session. With the help of session tracking when a client request for any pages or resources container creates a session id for that particular request and return back the session id to the client along with the response object.

When I Use HttpSessionListener

Whenever a session created or destroyed, servlet container will invoke HttpSessionListener

javax.servlet.http.HttpSessionListener interface has following methods:

sessionCreated(HttpSessionEvent event)
  It will notify when the session is created.
sessionDestroyed(HttpSessionEvent event) 
  It will notify when the session gets invalidated.

ServletContextListener Example

ServletContextListener Example explains about How to use ServletContextListener in a web application.

The listener ServletContextListener interface for receiving notification events about ServletContext lifecycle changes.

For the entire web application, there will be only one ServletContext. 

ServletContext is created when a new deployment is made after that this ServletContext will be used by all the jsp's and servlet's in the web application.

ServletContextListener has the following two methods:

public void contextInitialized(ServletContextEvent event)
public void contextDestroyed(ServletContextEvent event)

HttpSessionBindingListener Example

HttpSessionBindingListener Example explains about how to use HttpSessionBindingListener in a web application.

The listener HttpSessionBindingListener is an interface extending from base interface java.util.EventListener interface. This interface will notify an object when it is bound to or unbound from a session.

javax.servlet.http.HttpSessionBindingListener interface has following two methods:

valueBound(HttpSessionBindingEvent event).
valueUnBound(HttpSessionBindingEvent event).

You can see the below example, which is demonstrating HttpSessionBindingListener Example

Servlet Listener Example

Servlet Listener Example explains step by step details of implementing a Servlet Listener

Servlet Listener is helpful for listening various events inside a servlet container, such as add an attribute in a session, create a session, to manipulate these events you need to configure listener class in web.xml

Please see the following Java Servlet Listener Example

HttpSessionAttributeListener Example
HttpSessionBindingListener Example
HttpSessionListener Example
ServletContextAttributeListener Example
ServletContextListener Example

ServletContextAttributeListener Example

ServletContextAttributeListener Example Tutorial explains about how to use ServletContextAttributeListener in a web application.

The listener ServletContextAttributeListener is an interface extending from base interface java.util.EventListener.

ServletContextAttributeListener  will be notified when an attribute is changed on a particular servletcontext

ServletContextAttributeListener interface has following methods:

attributeAdded(ServletContextAttributeEvent event)
attributeRemoved(ServletContextAttributeEvent event)
attributeReplaced(ServletContextAttributeEvent event)

HttpSessionAttributeListener Example

HttpSessionAttributeListener Example explains about how to use HttpSessionAttributeListener in a web application.

HttpSessionAttributeListener is an interface which is extending from the base interface java.util.EventListener. HttpSessionAttributeListener will be informed by the web container when there is a change to the attributes of a web application's session, like an attribute is added in a session or an attribute is removed or an attribute is replaced by another attribute etc.

HttpSessionAttributeListener interface has following methods:

attributeAdded(HttpSessionBindingEvent event)
attributeRemoved(HttpSessionBindingEvent event)
attributeReplaced(HttpSessionBindingEvent event)