在下面的代码中 sessionCreated 在创建新会话时工作。但是 sessionDestroyed 只有在我们调用 session.invalidate() 时才有效;在以下情况下,我怎样才能使它工作**
1)用户关闭浏览器
2)系统断电
3)会话超时**
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SesListener implements HttpSessionListener
{
long sttime,endtime;
public void sessionCreated(HttpSessionEvent se)
{
sttime=System.currentTimeMillis();
HttpSession ses=se.getSession();
ServletContext sc=ses.getServletContext();
sc.log("session started at "+new Date());
System.out.println("session started at "+new Date());
}
public void sessionDestroyed(HttpSessionEvent se)
{
endtime=System.currentTimeMillis();
HttpSession ses=se.getSession();
ServletContext sc=ses.getServletContext();
sc.log("session ended at "+new Date());
System.out.println("session ended at "+new Date());
}
}
我的 .xml 文件是
<listener>
<listener-class>SesListener</listener-class>
</listener>