listeners

5
1 Listeners A listener is an event handler that the server invokes when certain events occur (e.g. web application initialization/shutdown, session created/timed out…) In design pattern terms – observer pattern: An observer (in this case the listener) is notified when an event occurs in the subject(server). Typical uses: Application-wide initialization routines Managing dependencies between data stored in context or session attributes Monitoring the running application (e.g. number of current sessions)

Upload: wren

Post on 05-Jan-2016

26 views

Category:

Documents


0 download

DESCRIPTION

Listeners. A listener is an event handler that the server invokes when certain events occur (e.g. web application initialization/shutdown, session created/timed out…) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Listeners

1

Listeners A listener is an event handler that the server invokes

when certain events occur (e.g. web application initialization/shutdown, session created/timed out…)

In design pattern terms – observer pattern: An observer (in this case the listener) is notified when an event occurs in the subject(server).

Typical uses: Application-wide initialization routines Managing dependencies between data stored in context or session

attributes Monitoring the running application (e.g. number of current

sessions)

Page 2: Listeners

2

Listeners – Cont.There are different kinds of listener, each corresponding to an

interface and a group pf events. Some of them are: ServletContextListener

Web application initialized / shut down

ServletRequestListener request handler starting / finishing

HttpSessionListener session created / invalidated

ServletContextAttributeListener context attribute added / removed / replaced

HttpSessionAttributeListener session attribute added / removed / replaced

Page 3: Listeners

3

Listeners – Cont.

To use a listener one simply implements the appropriate interface and registers the listener in the deployment descriptor.

As an example, the following listener monitors the current and maximum number of active sessions. This class implements both HttpSessionListener and

ServletContextListener such that it is notified when the application starts, to initialize the context attributes, and when a session is created or invalidated.

Page 4: Listeners

4

Example: SessionMonitor (1/2)import javax.servlet.*;import javax.servlet.http.*;

public class SessionMonitor implements HttpSessionListener, ServletContextListener { private int active = 0, max = 0;

public void contextInitialized(ServletContextEvent sce) { store(sce.getServletContext()); }

public void contextDestroyed(ServletContextEvent sce) {}

public void sessionCreated(HttpSessionEvent se) { active++; if (active>max) max = active; store(se.getSession().getServletContext()); }

Next Slide (Update Context Attributes)

Page 5: Listeners

5

Example: SessionMonitor (2/2)public void sessionDestroyed(HttpSessionEvent se) { active--; store(se.getSession().getServletContext()); }

private void store(ServletContext c) { c.setAttribute("sessions_active", new Integer(active)); c.setAttribute("sessions_max", new Integer(max)); }}

Registration in web.xml:

<listener>

<listener-class>SessionMonitor</listener-class><listener>

Now that we have the class, what must we

do?

Context Attributes