The session listener works on the creation of the web session and also destroys it. Here's a simple example that logs the two events:
@WebListener
public class SampleSessionListener implements HttpSessionListener {
private static final Logger logger = getLogger(SampleSessionListener.class.getName());
@Override
public void sessionCreated(HttpSessionEvent se) {
logger.info("MySessionListener.sessionCreated: " + se.getSession().getId());
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
logger.info("MySessionListener.sessionDestroyed: " + se.getSession().getId());
}
}
Each listener must declare the javax.servlet.annotation.WebListener annotation. The session listener must implement the javax.servlet.http.HttpSessionListener interface.