18
® IBM Software Group © 2007 IBM Corporation Servlet Listeners 4.1.0 .3

® IBM Software Group © 2007 IBM Corporation Servlet Listeners 4.1.0.3

Embed Size (px)

Citation preview

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

®

IBM Software Group

© 2007 IBM Corporation

Servlet Listeners

4.1.0.3

Page 2: ® IBM Software Group © 2007 IBM Corporation Servlet Listeners 4.1.0.3

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

Page 3: ® IBM Software Group © 2007 IBM Corporation Servlet Listeners 4.1.0.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

Page 4: ® IBM Software Group © 2007 IBM Corporation Servlet Listeners 4.1.0.3

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

Page 5: ® IBM Software Group © 2007 IBM Corporation Servlet Listeners 4.1.0.3

5

How to Create a Servlet Listener

Create a class the implements at least one of the six listener interfacesServletContextListenerServletContextAttributesListenerHttpSessionListenerHttpSessionAttributesListenerServletRequestListenerServletRequestAttributesListener

Implement methods in the interfaceMethods correspond to specific eventsCode 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

Page 6: ® IBM Software Group © 2007 IBM Corporation Servlet Listeners 4.1.0.3

6

Selecting Servlet Listener Interfaces

Select the listener interfaces to implement according to the objects and actions to monitor

Object Actions Interface

ServletContext CreateDestroy

javax.servlet.ServletContextListener

ServletContext Add attributeRemove attributeReplace attribute

javax.servlet.ServletContextAttributesListener

HttpSession Create Destroy

javax.servlet.http.HttpSessionListener

HttpSession Add attributeRemove attributeReplace attribute

javax.servlet.http.HttpSessionAttributesListener

ServletRequest CreateDestroy

javax.servlet.ServletRequestListener

ServletRequest Add attributeRemove attributeReplace attribute

javax.servlet.ServletRequestAttributesListener

Page 7: ® IBM Software Group © 2007 IBM Corporation Servlet Listeners 4.1.0.3

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

Page 8: ® IBM Software Group © 2007 IBM Corporation Servlet Listeners 4.1.0.3

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

Page 9: ® IBM Software Group © 2007 IBM Corporation Servlet Listeners 4.1.0.3

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

Page 10: ® IBM Software Group © 2007 IBM Corporation Servlet Listeners 4.1.0.3

10

Defining Listeners to the Web Application Listeners are defined in the Web Deployment Descriptor

<listener> element defines a listenerMore 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>

Page 11: ® IBM Software Group © 2007 IBM Corporation Servlet Listeners 4.1.0.3

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 0public 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 consolepublic void contextDestroyed(ServletContextEvent arg0) {

System.out.println("Max sessions: " +

getMaxSessions(arg0.getServletContext()));}

Page 12: ® IBM Software Group © 2007 IBM Corporation Servlet Listeners 4.1.0.3

12

Sample Servlet Listener Application (2 of 2)

// Called when a session is created // Increment session count and compare to max sessionspublic 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 countpublic void sessionDestroyed(HttpSessionEvent arg0) {

ServletContext sc = arg0.getSession().getServletContext();setCurrentSessions(sc, getCurrentSessions(sc) - 1 );}

}

Page 13: ® IBM Software Group © 2007 IBM Corporation Servlet Listeners 4.1.0.3

13

Creating New Servlet Listeners with Wizards(1 of 2)

Application Developer has a Create Listener wizard

Page 14: ® IBM Software Group © 2007 IBM Corporation Servlet Listeners 4.1.0.3

14

Creating New Servlet Listeners with Wizards(2 of 2)

Remove the interfaces you do not wish to implement

Page 15: ® IBM Software Group © 2007 IBM Corporation Servlet Listeners 4.1.0.3

15

Maintaining the Listener Definition

Application Developer lists listeners in the Listener area of the Variables tab of the Web Deployment Descriptor

Page 16: ® IBM Software Group © 2007 IBM Corporation Servlet Listeners 4.1.0.3

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.

Page 17: ® IBM Software Group © 2007 IBM Corporation Servlet Listeners 4.1.0.3

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.

Page 18: ® IBM Software Group © 2007 IBM Corporation Servlet Listeners 4.1.0.3

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