| Java FAQ | ||
| JSP FAQ | ||
| Servlet FAQ | ||
XyzWs Servlet FAQ:
When do I use HttpSessionListener?
Printer-friendly version |
Mail this to a friend
|
Advertisement
|
When do I use HttpSessionListener?
The To receive notification events, the implementation class must be configured in the deployment descriptor for the web application. This entry points the server to a class that will be called when a session is created or destroyed. The entry required is simple. All you need is a listener and listener-class element in the following format. The listener-class element must be a fully qualified class name. <listener> <listener-class>mypackage.MySessionListener</listener-class> </listener>
The
The following example demonstrates how these methods may be used:
package mypackage;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Date;
public class MyHttpSessionListener implements HttpSessionListener
{
public void sessionCreated(HttpSessionEvent se)
{
HttpSession session = se.getSession();
System.out.print(getTime() + " (session) Created:");
System.out.println("ID=" + session.getId() + " MaxInactiveInterval=" + session.getMaxInactiveInterval());
}
public void sessionDestroyed(HttpSessionEvent se)
{
HttpSession session = se.getSession();
// session has been invalidated and all session data (except Id)is no longer available
System.out.println(getTime() + " (session) Destroyed:ID=" + session.getId());
}
private String getTime()
{
return new Date(System.currentTimeMillis()).toString();
}
}
There is no distinct difference between session timeout and session invalidation
from the perspective of the session object. Other
The
Even if your session attribute implements |