userlistener.java
来自「:一个用JavaScript开发的用户管理系统。」· Java 代码 · 共 36 行
JAVA
36 行
package test;
import javax.servlet.http.HttpSession;
import javax.servlet.http.*;
import javax.servlet.ServletContext;
import javax.servlet.*;
import java.util.*;
public class UserListener implements HttpSessionListener {
ServletContext application = null;
public void sessionCreated(HttpSessionEvent e1){
application=e1.getSession().getServletContext();
e1.getSession().setMaxInactiveInterval(120);
int count=0;
if(application.getAttribute("count")!=null)
count=(Integer)application.getAttribute("count");
count++;
application.setAttribute("count",new Integer(count));
}
public void sessionDestroyed(HttpSessionEvent e2){
application=e2.getSession().getServletContext();
Vector vc=(Vector)application.getAttribute("online");
for(int i=0;i<vc.size();i++){
String tem=(String)vc.get(i);
if(tem.equals((String)e2.getSession().getAttribute("user"))){
vc.removeElementAt(i);
}
}
application.setAttribute("online",vc);
int count=0;
count=(Integer)application.getAttribute("count");
count--;
application.setAttribute("count",new Integer(count));
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?