Java Servlet Pages

Published: Wednesday, 16 March 2005

Java Servlet Pages and Servlets

Session Listeners

You can add event listeners on session creation and session deletion.

The following example creates a SessionVariables instance and adds it into the session

public class SessionVariablesSessionListener implements HttpSessionListener {
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {
        httpSessionEvent.getSession().setAttribute(SessionVariables.KEY, new SessionVariables());
    }

    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
    }
}

The webapp’s web.xml must also be updated:

   ...
  <listener>
    <description>Session listener</description>
    <display-name>Session listener</display-name>
    <listener-class>com.magicmonster.intranet.web.struts.SessionVariablesSessionListener</listener-class>
  </listener>
</web-app>

More Articles

Java Servlet Pages