Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 3 of 5
A listener interested in observing the ServletContext attribute lifecycle can implement the ServletContextAttributesListener interface, which has three methods:
void attributeAdded(ServletContextAttributeEvent e): Called when an attribute is added to a servlet context
void attributeRemoved(ServletContextAttributeEvent e): Called when an attribute is removed from a servlet context
void attributeReplaced(ServletContextAttributeEvent e): Called when an attribute is replaced by another attribute in a servlet context
The ServletContextAttributeEvent class extends ServletContextEvent, and adds getName() and getValue() methods so the listener can learn about the attribute being changed. That is useful because Web applications that need to
synchronize application state (context attributes) with something like a database can now do it in one place.
The session listener model is similar to the context listener model. In the session model, there's an HttpSessionListener interface with two methods:
void sessionCreated(HttpSessionEvent e): Called when a session is created
void sessionDestroyed(HttpSessionEvent e): Called when a session is destroyed (invalidated)
The methods accept an HttpSessionEvent instance with a getSession() method to return the session being created or destroyed. You can use all these methods when implementing an admin interface
that keeps track of all active users in a Web application.
The session model also has an HttpSessionAttributesListener interface with three methods. Those methods tell the listener when attributes change, and could be used, for example, by
an application that synchronizes profile data held in sessions into a database:
void attributeAdded(HttpSessionBindingEvent e): Called when an attribute is added to a session
void attributeRemoved(HttpSessionBindingEvent e): Called when an attribute is removed from a session
void attributeReplaced(HttpSessionBindingEvent e): Called when an attribute replaces another attribute in a session
As you might expect, the HttpSessionBindingEvent class extends HttpSessionEvent and adds getName() and getValue() methods. The only somewhat abnormal thing is that the event class is named HttpSessionBindingEvent, not HttpSessionAttributeEvent. That's for legacy reasons; the API already had an HttpSessionBindingEvent class, so it was reused. This confusing aspect of the API may be ironed out before final release.
A possible practical use of lifecycle events is a shared database connection managed by a context listener. You declare the listener in the web.xml as follows:
<listener>
<listener-class>
com.acme.MyConnectionManager
</listener-class>
</listener>
The server creates an instance of the listener class to receive events and uses introspection to determine what listener interface (or interfaces) the class implements. Bear in mind that because the listener is configured in the deployment descriptor, you can add new listeners without any code change. You could write the listener itself as something like this: