10.9.1 Session listeners


Some listener classes are defined in the Servlet 2.4 specification to listen for state changes of a session and its attributes. This allows greater control over interactions with sessions, leading programmers to monitor creation, deletion, and modification of sessions. Programmers can perform initialization tasks when a session is created, or clean up tasks when a session is removed. It is also possible to perform some specific tasks for the attribute when an attribute is added, deleted, or modified.

The following are the Listener interfaces to monitor the events associated with the HttpSession object:

  • javax.servlet.http.HttpSessionListener

    Monitor creation and deletion, including session timeout, of a session.

  • javax.servlet.http.HttpSessionAttributeListener

    Monitor changes of session attributes, such as add, delete, and replace.

  • javax.servlet.http.HttpSessionActivationListener

    Monitor activation and passivation of sessions. Useful to monitor if the session exists, whether in memory or not, when persistent session is used.

Listener interfaces and their methods...

    Target Event Interface Method
    session create HttpSessionListener sessionCreated()
    destroy HttpSessionListener sessionDestroyed()
    activate HttpSessionActivationListener sessionDidActivate()
    passivate HttpSessionActivationListener sessionWillPassivate()
    attribute add HttpSessionAttributeListener attributeAdded()
    remove HttpSessionAttributeListener attributeRemoved()
    replace HttpSessionAttributeListener attributeReplaced()

Next