81,122
社区成员




import java.sql.SQLException;
import java.util.Hashtable;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class Listener implements HttpSessionListener,
HttpSessionAttributeListener {
private Hashtable usertable = new Hashtable();
private String username = null;
public synchronized void sessionCreated(HttpSessionEvent arg0) {
arg0.getSession().setMaxInactiveInterval(1200);//设置超时时间
}
public synchronized void sessionDestroyed(HttpSessionEvent arg0) {
username = (String) usertable.get(arg0.getSession().getId());
usertable.remove(arg0.getSession().getId());
arg0.getSession().removeAttribute("userID");
String loginTime = arg0.getSession().getAttribute("loginTime").toString();
OperateDB operateDB = new OperateDB();
try {
operateDB.executeUpdate("delete from pdm_login_user where cuser_id = '"+username+"' and clogin_time = '"+loginTime+"'");
} catch (SQLException e) {
e.printStackTrace();
}
}
public synchronized void attributeAdded(HttpSessionBindingEvent arg0) {
if (arg0.getSession().getAttribute("userID") != null) {
usertable.put(arg0.getSession().getId(), arg0.getSession()
.getAttribute("userID"));
}
}
public synchronized void attributeRemoved(HttpSessionBindingEvent arg0) {
}
public synchronized void attributeReplaced(HttpSessionBindingEvent arg0) {
}
}