Presentation is loading. Please wait.

Presentation is loading. Please wait.

® IBM Software Group © 2007 IBM Corporation Servlet Listeners 4.1.0.3.

Similar presentations


Presentation on theme: "® IBM Software Group © 2007 IBM Corporation Servlet Listeners 4.1.0.3."— Presentation transcript:

1 ® IBM Software Group © 2007 IBM Corporation Servlet Listeners 4.1.0.3

2 2 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 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 Unit objectives

3 3 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

4 4 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

5 5 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 element defines the listener

6 6 Selecting Servlet Listener Interfaces  Select the listener interfaces to implement according to the objects and actions to monitor ObjectActionsInterface ServletContextCreate Destroy javax.servlet. ServletContextListener ServletContextAdd attribute Remove attribute Replace attribute javax.servlet. ServletContextAttributesListener HttpSessionCreate Destroy javax.servlet.http. HttpSessionListener HttpSessionAdd attribute Remove attribute Replace attribute javax.servlet.http. HttpSessionAttributesListener ServletRequestCreate Destroy javax.servlet. ServletRequestListener ServletRequestAdd attribute Remove attribute Replace attribute javax.servlet. ServletRequestAttributesListener

7 7 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

8 8 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

9 9 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

10 10 Defining Listeners to the Web Application  Listeners are defined in the Web Deployment Descriptor  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 com.ibm.library.listeners.SessionCounter com.ibm.library.listeners.LoggerListener

11 11 Sample Servlet Listener Application (1 of 2) public class SessionCounter implements ServletContextListener, HttpSessionListener { // Called when web app ready to process requests // Initialize current session and max session counts to 0 public void 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 public void contextDestroyed(ServletContextEvent arg0) { System.out.println("Max sessions: " + getMaxSessions(arg0.getServletContext())); }

12 12 Sample Servlet Listener Application (2 of 2) // Called when a session is created // Increment session count and compare to max sessions public void 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 public void sessionDestroyed(HttpSessionEvent arg0) { ServletContext sc = arg0.getSession().getServletContext(); setCurrentSessions(sc, getCurrentSessions(sc) - 1 ); }

13 13 Creating New Servlet Listeners with Wizards(1 of 2)  Application Developer has a Create Listener wizard

14 14 Creating New Servlet Listeners with Wizards(2 of 2)  Remove the interfaces you do not wish to implement

15 15 Maintaining the Listener Definition  Application Developer lists listeners in the Listener area of the Variables tab of the Web Deployment Descriptor

16 16 Checkpoint 1.Define a Servlet Listener. 2.What are the lifecycle actions that can be monitored by a servlet listener for the ServletContext object? 3.What are the attribute actions that can be monitored by a servlet listener for a ServletContext object? 4.What are the lifecycle actions that can be monitored by a servlet listener for the HttpSession object? 5.What are the attribute actions that can be monitored by a servlet listener for a HttpSession object? 6.Name the six interfaces that can be implemented to construct a Servlet Listener class.

17 17 Checkpoint solutions 1.A class that can listen and react to certain types of events and state changes in a Web application. 2.Creation and destruction of the servlet context. 3.Removal, addition, and replacement of a servlet context attribute. 4.Creation and destruction of a HTTP session. 5.Removal, addition, and replacement of a session attribute. 6.The six interfaces are ServletContextListener, ServletContextAttributesListener, HttpSessionListener, HttpSessionAttributesListener, ServletRequestListener and ServletRequestAttributeListener.

18 18 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 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 Unit summary


Download ppt "® IBM Software Group © 2007 IBM Corporation Servlet Listeners 4.1.0.3."

Similar presentations


Ads by Google