Using the feature of Listener introduced in Servlet, we can make a web application which responds to events taking place in a web application.
These events can be of request, session or application level scope. On the basis of these different types of events,
there are several Listener interfaces defined under javax.servlet package.
Listener Interfaces
Some of the most important Listener interfaces are -
ServletContextListener
ServletContextAttributeListener
HttpSessionListener
HttpSessionAttributeListener
HttpSessionBindingListener
ServletRequestListener
ServletRequestAttributeListener
ServletContextListener
By implementing ServletContextListener interface, we can listen to the events -
When the ServletContext object is created in a web application.
When the ServletContext object is about to be destroyed.
Doing so, we can initialize the objects in servlet context of the web application and destroy these objects when servlet context of a web application is going to be destroyed.
ServletContextAttributeListener
By implementing ServletContextAttributeListener interface, we can listen to these events -
When an attribute is added to the ServletContext object in a web application.
When an attribute of the ServletContext object is modified in a web application.
When an attribute of the ServletContext object is deleted in a web application.
HttpSessionListener
By implementing HttpSessionListener interface, we can listen to these events -
When an HttpSession is created in a web application.
When an HttpSession is invalidated in a web application.
Doing so, we can initialize the objects in HttpSession of the web application and destroy these objects when HttpSession of a web application is going to be invalidated.
Advertisement
HttpSessionAttributeListener
By implementing HttpSessionAttributeListener interface, we can listen to these events -
When an attribute is added in an HttpSession witin a web application.
When an attribute is replaced in an HttpSession within a web application.
When an attribute is deleted in an HttpSession within a web application.
HttpSessionBindingListener
By implementing HttpSessionBindingListener interface, we can listen to these events -
When an attribute of HttpSessionBindingListener type is added in an HttpSession within a web application.
When an attribute of HttpSessionBindingListener type is replaced in an HttpSession within a web application.
When an attribute of HttpSessionBindingListener type is deleted in an HttpSession within a web application.
ServletRequestListener
By implementing ServletRequestListener interface, we can listen to these events -
When a user request reaches the web application.
When a user request is served by the web application.
ServletRequestAttributeListener
By implementing ServletRequestAttributeListener interface, we can listen to these events -
When an attribute is added to an ServletRequest object within a web application.
When an attribute of an ServletRequest object is replaced in within a web application.
When an attribute of an ServletRequest object is deleted in within a web application.