📄 onlinelistener.java
字号:
package MyPackage;
import java.util.ArrayList;
import java.util.List;
import MyPackage.UserBean;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class OnlineListener implements ServletContextListener,ServletContextAttributeListener,HttpSessionListener,HttpSessionAttributeListener {
private ServletContext application = null ;
public void contextInitialized(ServletContextEvent sce) {
this.application=sce.getServletContext() ; //初始化一个application对象
//设置一个列表属性,用于保存在线用户名
this.application.setAttribute("online", new ArrayList()) ; }
public void sessionDestroyed(HttpSessionEvent se) {
List online=(List)this.application.getAttribute("online") ; //取得用户列表
UserBean user = (UserBean)se.getSession().getAttribute("user");//取得当前用户
online.remove(user) ; //将此用户名从列表中删除
//将删除后的列表重新设置到application属性中
this.application.setAttribute("online", online) ;
}
public void attributeAdded(HttpSessionBindingEvent se) {
List online=(List)this.application.getAttribute("online") ; //取得用户列表
online.add(se.getValue()); //将当前用户添加到列表中
this.application.setAttribute("online", online) ; //将添加后的列表重新设置到application属性中
}
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
}
public void attributeAdded(ServletContextAttributeEvent arg0) {
// TODO Auto-generated method stub
}
public void attributeRemoved(ServletContextAttributeEvent arg0) {
// TODO Auto-generated method stub
}
public void attributeReplaced(ServletContextAttributeEvent arg0) {
// TODO Auto-generated method stub
}
public void sessionCreated(HttpSessionEvent arg0) {
}
public void attributeRemoved(HttpSessionBindingEvent arg0) {
// TODO Auto-generated method stub
}
public void attributeReplaced(HttpSessionBindingEvent arg0) {
// TODO Auto-generated method stub
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -