Don’t forget about HttpSessionBindingListener

image with no caption

The events on the previous page are for key moments in the life of the session. But the HttpSessionBindingListener is for key moments in the life of a session attribute. Remember from Chapter 5 where we looked at how you might use this—if, for example, your attribute wants to know when it’s added to a session so that it can synchronize itself with an underlying database (and update the database when it’s removed from a session). Here’s a little review from the previous chapter:

image with no caption

Note

You do NOT configure ALL session listeners in the DD!

If an attribute class (like the Dog class here) implements the HttpSessionBindingListener, the Container calls the event-handling callbacks (valueBound() and valueUnbound()) when an instance of this class is added to or removed from a session. That’s it. It just works. But this is NOT true for the other session-related listeners on the previous page. HttpSessionListener and HttpSessionAttributeListener must be registered in the DD, since they’re related to the session itself, rather than an individual attribute placed in the session.

Session migration

Remember from the previous chapter, we talked briefly about distributed web apps, where the pieces of the app might be replicated across multiple nodes in the network. ...

Get Head First Servlets and JSP, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.