An Event are basically occurrence of something. Changing the state of an object is known as an event.
A listener receives the notifications about changes in the Servlet and perform some action.
We can perform some important task at the occurrence of these events, such as:
- Counting total and current logged users.
- Creating tables of the database at the time of deploying the project.
- Creating database connection object, and etc..
Various Listener Interfaces are:
1. Servlet RequestAtrributeListener
This will give a notification about the changes to the attributes of ServletRequest in an application.
Methods:
void attributeadded(servletrequestattributeevent e)
void attributeremoved(servletrequestattributeevent e)
void attributereplaced(servletrequestattributeevent e)
2. HttpSessionAttributeListener
This will give a notification the changes (added, removed, and replaced) on the attributes in any session on the server.
Methods:
void attributeAdded(HttpSessionBindingEvent event)
void attributeRemoved(HttpSessionBindingEvent event)
void attributeReplaced(HttpSessionBindingEvent event)
3. ServletContextAtributeListener
This will give a notification, whenever the web application context has been added, removed or replaced.
Methods:
void attributeAdded(ServletContextAttributeEvent e)
void attributeRemoved(ServletContextAttributeEvent e)
void attributeReplaced(ServletContextAttributeEvent e)
4. Servlet RequestListener
This will give a notification about life cycle events for a ServletRequest
Methods:
void requestDestroyed(ServletRequestEvent e)
void requestInitialized(ServletRequestEvent e)
5. HttpSessionListener
This will give a notification for changes to sessions within a web application. HttpSessionListener is used to perform some important tasks when a session is created or destroyed. For example: counting the number of active sessions.
Methods:
void sessionCreated(HttpSessionEvent e)
void sessionDestroyed(HttpSessioEvent e)
6. ServletContextListener
This will give a notification about the changes to the servlet context of a web application.
Methods:
void contextDestroyed(ServletContextEvent e)
void contextInitialized(ServletContextEvent e)
7. HTTPSessionBindingListener
This interface will notify an object when it is bound to or unbound from a session.
Methods:
void valueBound(HttpSessionBindingEvent event).
void valueUnBound(HttpSessionBindingEvent event).
8. HTTPSessionActivationListener
The HttpSessionActivationListener
is used for responding to events when a session object migrates from one virtual machine to another. When a session is activated, notification that the session has just been activated will be received by theSessionDidActivate
method and when the session is about to be destroyed, thesessionWillPassivate
method is invoked.
Methods:
public void sessionDidActivate(HttpSessionEvent se)
public void sessionWillPassivate(HttpSessionEvent se)