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
public ServletContext getServletContext();
public void contextDestroyed(ServletContextEvent sce);
public void contextlnitialized(ServletContextEvent sce);
public HttpSession getSession();
public void sessionCreated(HttpSesionEvent hse);
public void sessionDestroyed(HttpSessionEvent hse);
public ServletRequest getServletRequest();
requestInitialized(ServletRequestEvent sre);
requestDestroyedlServletRequestEvent sre);
public String getName();
public Object getVaiue();
public void attributeAdded(ServletContextAttributeEvent sce);
public void attributeRemoved(ServletContextAttributeEvent sce);
public void attributeReplaced(ServletContextAttributeEvent sce);
public String getName();
public Object getVaiue();
public void attributeAdded(HttpSessionBindingEvent sce);
public void attributeRemoved(HttpSessionBindingEvent sce);
public void attributeReplaced(HttpSessionBindingEvent sce);
public String getName();
public Object getValue();
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 *