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. In a clustered environment, the Container might do load-balancing by taking client requests and sending them out to JVMs (which may or may not be on different physical boxes, but that doesn’t matter to us). The point is, the app is in multiple places.

That means each time the same client makes a request, the request could end up going to a different instance of the same servlet. In other words, request A for Servlet A could happen on one VM, and request B for Servlet A could end up on a different VM. So the question is, what happens to things like ServletContext, ServletConfig, and HttpSession objects?

Simple answer, important implications:

Only HttpSession objects (and their attributes) move from one VM to another.

There is one ServletContext per VM. There is one ServletConfig per servlet, per VM. But there is only one HttpSession object for a given session ID per web app, regardless of how many VM’s the app is distributed across.

The Beer Web App distributed across two VMs

image with no caption

Session migration in action

How an app server vendor handles clustering and web app distribution varies with each vendor, and there’s no guarantee in the J2EE spec that a vendor has to support distributed apps. But the picture here gives you a high-level idea of how it works. The key point is that while other parts of the app are replicated on each node/VM, the session objects are moved. And that is guaranteed. In other words, if the vendor does support distributed apps, then the Container is required to migrate sessions across VMs. And that includes migrating session attributes as well.

  1. image with no caption
  2. image with no caption
  3. image with no caption
  4. image with no caption
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset