210 likes | 458 Views
Servlet Listeners. 4.1.0.3. Unit objectives. After completing this unit, you should be able to: Define Servlet Listeners and how they can be used in a Web application List typical uses of Servlet Listeners Describe the major steps used to create a Servlet Listener
E N D
Servlet Listeners 4.1.0.3
Unit objectives After completing this unit, you should be able to: • Define Servlet Listeners and how they can be used in a Web application • List typical uses of Servlet Listeners • Describe the major steps used to create a Servlet Listener • Name the six interfaces used to create Servlet Listeners and when to use each interface • List the methods defined for each of the Servlet Listener interfaces and their use • Describe how to define a Servlet Listener to the Web application • State the rules for determining the order of execution of multiple Listeners in a Web application • Describe the facilities of Application Developer used for the development of Listeners
Basic Concepts of Servlet Event Listeners • What is a Servlet Event Listener? • A class that can listen and react to certain types of events and state changes in a Web application • Allows developers to: • Let Listener objects listen for Web module state changes: • ServletContext lifecycle: Creation and destruction • ServletContext attributes: Addition, replacement, and removal • HttpSession lifecycle: Creation and destruction • HttpSession attributes: Addition, replacement, and removal • ServletRequest lifecycle: Creation and destruction • ServletRequest attributes: Addition, replacement, and removal • Execute actions in response to the events • Advantages: • More control over interactions with application, session and request objects • Centralized monitoring and response to events
Examples of Servlet Listener Use • Examples: • Monitor start and stop of Web modules to perform startup and shutdown tasks • Add attributes to ServletContext or HttpSession objects on creation • Monitor creation and destruction of sessions • Log important application events • Sample Scenario: • When application starts, listener is notified and creates a connection to the database. Connection is stored in servlet context attribute • Servlets access the connection as needed from the servlet context • When the Web application is shutdown, listener is notified and closes database connection
How to Create a Servlet Listener • Create a class the implements at least one of the six listener interfaces • ServletContextListener • ServletContextAttributesListener • HttpSessionListener • HttpSessionAttributesListener • ServletRequestListener • ServletRequestAttributesListener • Implement methods in the interface • Methods correspond to specific events • Code logic to respond to events • Create a public zero-argument constructor for the class • Add the listener to the Web Deployment Descriptor • A <listener> element defines the listener
Selecting Servlet Listener Interfaces • Select the listener interfaces to implement according to the objects and actions to monitor
Selecting Methods for ServletContext Events • To monitor lifecycle events (ServletContextListener interface) use: • contextInitialized(ServletContextEvent e) • Called when Web application is ready to process requests • contextDestroyed(ServletContextEvent e) • Called when Web application is about to be shut down • To monitor attribute events (ServletContextAttributesListener interface) use: • attributeAdded(ServletContextAttributeEvent e) • Called after an attribute is added to a ServletContext • attributeRemoved(ServletContextAttributeEvent e) • Called after an attribute is removed from a ServletContext • attributeReplaced(ServletContextAttributeEvent e) • Called after an attribute is replaced by another in a ServletContext
Selecting Methods for HttpSession Events • To monitor lifecycle events (HttpSessionListener interface), use: • sessionCreated(HttpSessionEvent e) • Called when a session is created • sessionDestroyed(HttpSessionEvent e) • Called when a session is destroyed • To monitor attribute events (HttpSessionAttributesListener interface), use: • attributeAdded(HttpSessionBindingEvent e) • Called after an attribute is added to a session • attributeRemoved(HttpSessionBindingEvent e) • Called after an attribute is removed from a session • attributeReplaced(HttpSessionBindingEvent e) • Called after an attribute is replaced by another in a session
Selecting Methods for ServletRequest Events • To monitor lifecycle events (ServletRequestListener interface) use: • requestInitialized(ServletRequestEvent e) • Called when the request is about to come into scope of the Web application • requestDestroyed(ServletRequestEvent e) • Called when the request is about to go out of scope of the Web application • To monitor attribute events (ServletRequestAttributesListener interface) use: • attributeAdded(ServletRequestAttributeEvent e) • Called after an attribute is added to a ServletRequest • attributeRemoved(ServletRequestAttributeEvent e) • Called after an attribute is removed from a ServletRequest • attributeReplaced(ServletRequestAttributeEvent e) • Called after an attribute is replaced by another in a ServletRequest
Defining Listeners to the Web Application • Listeners are defined in the Web Deployment Descriptor • <listener> element defines a listener • More than one listener may be defined • Container executes listeners in order they appear in the deployment descriptor file (web.xml) • Listener class file is placed in WEB-INF/classes or packaged in JAR file in WEB-INF/lib <listener> <listener-class> com.ibm.library.listeners.SessionCounter </listener-class> </listener> <listener> <listener-class> com.ibm.library.listeners.LoggerListener </listener-class> </listener>
publicclass SessionCounter implements ServletContextListener, HttpSessionListener { // Called when web app ready to process requests // Initialize current session and max session counts to 0 publicvoid contextInitialized(ServletContextEvent arg0) { ServletContext sc = arg0.getServletContext(); setCurrentSessions(sc, 0); setMaxSessions(sc, 0); } // Called when web application is about to be shutdown // Print out max session count at the console publicvoid contextDestroyed(ServletContextEvent arg0) { System.out.println("Max sessions: " + getMaxSessions(arg0.getServletContext())); } Sample Servlet Listener Application (1 of 2)
Sample Servlet Listener Application (2 of 2) // Called when a session is created // Increment session count and compare to max sessions publicvoid sessionCreated(HttpSessionEvent arg0) { ServletContext sc = arg0.getSession().getServletContext(); int currentSessions = getCurrentSessions(sc)+ 1; int maxSessions = getMaxSessions(sc); if (currentSessions > maxSessions) { setMaxSessions(sc, currentSessions); } setCurrentSessions(sc, currentSessions); } // Called when a session is destroyed // Decrement session count publicvoid sessionDestroyed(HttpSessionEvent arg0) { ServletContext sc = arg0.getSession().getServletContext(); setCurrentSessions(sc, getCurrentSessions(sc) - 1 ); } }
Creating New Servlet Listeners with Wizards(1 of 2) • Application Developer has a Create Listener wizard
Creating New Servlet Listeners with Wizards(2 of 2) • Remove the interfaces you do not wish to implement
Maintaining the Listener Definition • Application Developer lists listeners in the Listener area of the Variables tab of the Web Deployment Descriptor
Checkpoint • Define a Servlet Listener. • What are the lifecycle actions that can be monitored by a servlet listener for the ServletContext object? • What are the attribute actions that can be monitored by a servlet listener for a ServletContext object? • What are the lifecycle actions that can be monitored by a servlet listener for the HttpSession object? • What are the attribute actions that can be monitored by a servlet listener for a HttpSession object? • Name the six interfaces that can be implemented to construct a Servlet Listener class.
Checkpoint solutions • A class that can listen and react to certain types of events and state changes in a Web application. • Creation and destruction of the servlet context. • Removal, addition, and replacement of a servlet context attribute. • Creation and destruction of a HTTP session. • Removal, addition, and replacement of a session attribute. • The six interfaces are ServletContextListener, ServletContextAttributesListener, HttpSessionListener, HttpSessionAttributesListener, ServletRequestListener and ServletRequestAttributeListener.
Unit summary Having completed this unit, you should be able to: • Define Servlet Listeners and how they can be used in a Web application • List typical uses of Servlet Listeners • Describe the major steps used to create a Servlet Listener • Name the six interfaces used to create Servlet Listeners and when to use each interface • List the methods defined for each of the Servlet Listener interfaces and their use • Describe how to define a Servlet Listener to the Web application • State the rules for determining the order of execution of multiple Listeners in a Web application • Describe the facilities of Application Developer used for the development of Listeners