JavaServer Pages (JSP) Lifecycle Listeners Primary Reference: Marty Hall, Larry Brown, and Yaakov Chaikin, Core Servlets and JavaServer Pages, Volume 2: Advanced Technologies (Second Edition), Prentice Hall, 2008. ©SoftMoore Consulting
JavaServer Pages (JSP) Motivation Respond to events in the overall application lifecycle. notice when certain attributes are removed from the servlet context Perform tasks more general than any one servlet or JSP page has responsibility for. insert application-wide data into the servlet context before any servlets or JSP pages are accessed keep track of certain session attributes, regardless of which servlet or JSP page added the attributes Lifecycle listeners respond to web application lifecycle events ©SoftMoore Consulting
Lifecycle Listeners A listener is a class that “listens” for and can respond to various events that can occur during the lifecycle of a web application. A listener must extend one of the “listener” interfaces in package javax.servlet or package javax.servlet.http. Listeners must be configured in the deployment descriptor for the web application. ©SoftMoore Consulting
Example Listener Interfaces ServletContextListener void contextInitialized(ServletContextEvent sce) void contextDestroyed(ServletContextEvent sce) ServletRequestListener void requestInitialized(ServletRequestEvent sre) void requestDestroyed(ServletRequestEvent sre) HttpSessionListener void sessionCreated(HttpSessionEvent se) void sessionDestroyed(HttpSessionEvent se) ©SoftMoore Consulting
Kinds of Lifecycle Listeners Servlet context listeners – notified when the servlet context (i.e., the web application) is initialized and destroyed. Servlet context attribute listeners – notified when attributes are added to, removed from, or replaced in the servlet context Session listeners – notified when session objects are created, invalidated, or timed out Session attribute listeners – notified when attributes are added to, removed from, or replaced in any session ©SoftMoore Consulting
Kinds of Lifecycle Listeners (continued) Session migration listeners – notified when session objects are serialized and deserialized by the container Session object binding listeners – notified when the implementing object is added to or removed from the session object Request listeners – notified when request objects are initialized or destroyed Request attribute listeners – notified when attributes are added to, removed from, or replaced in any request ©SoftMoore Consulting
Implementing a Lifecycle Listener Implement the appropriate interface – ServletContextListener – ServletContextAttributeListener – HttpSessionListener – HttpSessionAttributeListener – HttpSessionBindingListener − HttpSessionActivationListener – ServletRequestLIstener – ServletRequestAttributeListener Implement the methods needed to respond to the events of interest Provide empty bodies for the other methods in the interface. Obtain access to the important Web application objects – name of the servlet context attribute that changed – value of the servlet context attribute that changed – request /session object – name of the session attribute that changed ©SoftMoore Consulting
Implementing a Lifecycle Listener (continued) Use the objects. For example, use the servlet context to read initialization parameters, method getInitParameter() store data for later access, method setAttribute() read previously stored data, method getAttribute() Declare the listener. Add listener and listener-class elements to the application deployment descriptor (web.xml) or of a tag library descriptor file. Provide any needed initialization parameters. Add context-param elements to the application deployment descriptor (web.xml) to provide initialization parameters for servlet context listeners ©SoftMoore Consulting
Example: ContextListener package com.softmoore.servlet; import com.softmoore.process.DatabaseUpdateThread; import com.softmoore.util.Log; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class ContextListener implements ServletContextListener { private DatabaseUpdateThread updateThread; public ContextListener() super(); updateThread = new DatabaseUpdateThread(); } ©SoftMoore Consulting
Example: ContextListener (continued) @Override public void contextInitialized(ServletContextEvent event) { updateThread.start(); Log log = Log.getInstance(); log.logMessage("DatabaseUpdateThread started"); } public void contextDestroyed(ServletContextEvent event) updateThread.interrupt(); log.logMessage("DatabaseUpdateThread terminated"); ©SoftMoore Consulting
Declaring a Listener in the Deployment Descriptor <?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/..." version="2.5"> ... <listener> <listener-class> edu.citadel.cis.servlet.ContextListener </listener-class> </listener> </web-app> ©SoftMoore Consulting
Using Annotations to Declare Listeners Introduced in servlet API version 3.0, the annotation @WebListener annotation can be used to register a class as a listener of a web application. syntax: @WebListener(optional description) Example @WebListener public class ContextListener implements ServletContextListener { ... } Note that the class must still implement one of the listener interfaces. ©SoftMoore
Using Annotations to Declare/Map Filters (continued) Annotation @WebListener replaces the equivalent <listener> configuration element in the deployment descriptor file (web.xml) Servlet containers process the annotated classes when the application is deployed. Note: Several additional annotations were also introduced in servlet API 3.0; e.g., @WebServlet and @WebFilter. ©SoftMoore