Listeners

Listeners :

This functionality allows more efficient resource management and automated processing based on event status. Event is occurrence of something in web application world an event can be initialization of application, destroying an application, request from client, creating/destroying a session, attribute modification in session etc.
Servlet APl provides different types of Listener interfaces that we can implement and configure in web.xml to process something when a particular event occurs.
Event Listener
 
EVENT
LISTENER
ServletContextEvent
public ServletContext getServletContext();
ServletContextListener
public void contextDestroyed(ServletContextEvent sce);
public void contextlnitialized(ServletContextEvent sce);
HttpSessionEvent
public HttpSession getSession();
HttpSessionListener
public void sessionCreated(HttpSesionEvent hse);
public void sessionDestroyed(HttpSessionEvent hse);
ServletRequestEvent
public ServletRequest getServletRequest();
ServletRequestListener
requestInitialized(ServletRequestEvent sre);
requestDestroyedlServletRequestEvent sre);
ServletContextAttributeEvent
public String getName();
public Object getVaiue();
ServletContextAttributelistener
public void attributeAdded(ServletContextAttributeEvent sce);
public void attributeRemoved(ServletContextAttributeEvent sce);
public void attributeReplaced(ServletContextAttributeEvent sce);
HttpSessionBindingEvent
public String getName();
public Object getVaiue();
HttpSessionBindingListener
public void attributeAdded(HttpSessionBindingEvent sce);
public void attributeRemoved(HttpSessionBindingEvent sce);
public void attributeReplaced(HttpSessionBindingEvent sce);
ServletRequestAttributeEvent
public String getName();
public Object getValue();
ServletRequestAttributeListener
public void attributeAdded(ServletRequestAttributeEvent sce);
public void attributeRemoved(ServletRequestAttributeEvent sce);
public void attributeReplaced(ServletRequestAttributeEvent sce);
Event listeners are declared in the application web.xml deployment descriptor through <listener> elements under the top-level <web-app> element. Each listener has its own <listener> element, with a <listener-class> sub element specifying the class name. Within each event category, event listeners should be specified in the order in which you would like them to be invoked when the application runs.
After the application starts up and before it services the first request, the servlet container creates and registers an instance of each listener class that has been declared. For each event category, listeners are registered in the order in which they are declared. Then, as the application runs, event listeners for each category are invoked in the order of their registration. All listeners remain active until after the last request is serviced for the application.
The listener classes are packaged into the WAR, either under the WEB-INF/classes archive entry, or inside a JAR in the WEB-INF/libdirectory.

Leave a Reply

Your email address will not be published. Required fields are marked *