HttpSessionActivationListener lets attributes prepare for the big move...

image with no caption

Since it’s possible that an HttpSession can migrate from one VM to another, the spec designers thought it would be nice if someone bothered to tell the attributes within the session that they, too, were about to move. That way the attributes can make sure they’ll survive the trip.

If all your attributes are straightforward Serializable objects that don’t care where they end up, you’ll probably never use this listener. In fact, we’re guessing 95.324% of all web apps never use this listener. But it’s there if you need it, and the most likely use of this listener is to give attributes a chance to make their instance variables ready for Serialization.

image with no caption

Session migration and Serialization

Now it gets a little tricky...

A Container is required to migrate Serializable attributes (which assumes that all instance variables within the attribute are either Serializable or null).

But a Container is not required to use Serialization as the means for migrating the HttpSession object!

What does this mean to you? Simple: make sure your attribute class types are Serializable and you never have to worry about it. But if they’re not Serializable (which could be because one of the attribute object’s instance variables is not Serializable), have your attribute object class implement HttpSessionActivationListener and use the activation/passivation callbacks to work around it.

Note

The Container is not REQUIRED to use Serialization, so there’s no guarantee that readObject() and writeObject() will be called on a Serializable attribute or one of its instance variables!

If you’re familiar with Serialization, you know that a class that implements Serializable can also choose to implement a writeObject() method, called by the VM whenever an object is serialized, and a readObject() method, called when an object is deserialized. A Serializable object can use these methods to, for example, set non-Serializable fields to null during Serialization (writeObject()) and then restore the fields during deserialization (readObject()). (If you’re NOT familiar with the details of Serialization, don’t worry about it.) But the methods won’t necessarily be called during session migration! So if you need to save and restore instance variable state in your attribute, use HttpSessionActivationListener, and use the two event call-backs (sessionDidActivate() and sessionWillPassivate()) the way you’d use readObject() and writeObject().

Listener examples

Over the next three pages, pay attention to the event object types and to whether the listener is also an attribute class.

Session counter

This listener lets you keep track of the number of active sessions in this web app. Very simple.

image with no caption

Configuring the listener in the DD

<web-app ...>
  ...
   <listener>
     <listener-class>
       com.example.BeerSessionCounter
     </listener-class>
   </listener>
</web-app>

Note

FYI- this wouldn’t work correctly if the app is distributed on multiple JVMs, because there is no way to keep the static variables in sync. If the class is loaded on more than one JVM, each class will have its own value for the static counter variable.

Listener examples

Attribute Listener

This listener lets you track each time any attribute is added to, removed from, or replaced in a session.

image with no caption

Configuring the listener in the DD

<web-app ...>
  ...
   <listener>
     <listener-class>
       com.example.BeerAttributeListener
     </listener-class>
   </listener>
</web-app>

Q:

Q: Hey, what the heck are you printing to? Where does System.out go in a web app?

A:

A: Wherever this Container chooses to send it (which may or may not be configurable by you). In other words, in a vendor-specific place, often a log file. Tomcat puts the output in tomcat/logs/catalina.log. You’ll have to read your server docs to find out what your Container does with standard output.

Listener examples

Attribute class (listening for events that affect IT)

This listener lets an attribute keep track of events that might be important to the attribute itself—when it’s added to or removed from a session, and when the session migrates from one VM to another.

image with no caption

Session-related Listeners

Scenario

Listener interface/methods

Event type

Usually implemented by

You want to know how many concurrent users there are. In other words, you want to track the active sessions.

HttpSessionListener (javax.servlet.http)

sessionCreated

sessionDestroyed

HttpSessionEvent

An attribute class

Some other class

You want to know when a session moves from one VM to another.

HttpSessionActivationListener (javax.servlet.http)

sessionDidActivate

sessionWillPassivate

HttpSessionEvent

Note

Note: there’s no specific HttpSessionActivationEvent.

An attribute class

Some other class

You have an attribute class (a class for an object that will be used as an attribute value) and you want objects of this type to be notified when they are bound to or removed from a session.

HttpSessionBindingListener (javax.servlet.http)

valueBound

valueUnbound

HttpSessionBindingEvent

An attribute class

Some other class

You want to know when any session attribute is added, removed, or replaced in a session.

HttpSessionAttributeListener (javax.servlet.http)

attributeAdded

attributeRemoved

attributeReplaced

HttpSessionBindingEvent

Note

Note: there’s no specific HttpSessionAttributeEvent.

An attribute class

Some other class

Note

Some of the session-related events don’t follow the event naming conventions!

HttpSessionListener methods take Http SessionEvents. HttpSessionBindingListener methods take HttpSession BindingEvents. BindingEvents. But HttpSessionAttributeListener methods take HttpSession And HttpSessionActivationListener methods take Http SessionEvents. Since HttpSessionEvent and HttpSessionBindingEvent classes worked perfectly well, there was no need for the API to add two more event classes.

Session-related Event Listeners and Event Objects API overview

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