Wednesday, May 31, 2006

Listening to HttpSession Events

The javax.servlet.http package provides two interfaces that you can implement to listen to HttpSession events: HttpSessionListener and HttpSessionAttributeListener.
The first enables you to listen to a session's life cycle events, that is, the event that gets triggered when an HttpSession object is created and the event that is raised when an HttpSession object is destroyed. The second interface, HttpSessionAttributeListener, provides events that get raised when an attribute is added to, removed from, or modified in the HttpSession object.

The HttpSessionListener Interface :-

public void sessionCreated(HttpSessionEvent se)
public void sessionDestroyed(HttpSessionEvent se)

The HttpSessionEvent class is derived from the java.util.EventObject class. The HttpSessionEvent class defines one new method called getSesssion that you can use to obtain the HttpSession object that is changed.

The HttpSessionAttributeListener Interface :-

public void attributeAdded(HttpSessionBindingEvent sbe)
public void attributeRemoved(HttpSessionBindingEvent sbe)
public void attributeReplaced(HttpSessionBindingEvent sbe)

The HttpSessionBindingEvent is derived from the HttpSessionEvent class so it inherits the getSession method. In addition, the HttpSessionBindingEvent class has two methods: getName and getValue. The signatures of the two methods are as follows:

public String getName()
public Object getValue()

The getName method returns the name of the attribute that is bound to an HttpSession or unbound from an HttpSession. The getValue method returns the value of an HttpSession attribute that has been added, removed, or replaced.

0 Comments:

Post a Comment

<< Home