HttpSessionListener实现人数在线统计
程序员文章站
2022-07-13 17:49:18
...
现在正在做一个用户在线列表(已登陆),想用HttpSessionListener接口实现。
但发现好像HttpSessionListener只能统计在线人数。
不知有什么方法能实现? 就像聊天室里的那个在线列表一样的。
---------------------------------------------------------------
package SessionCounter; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; import com.oztime.right.UserObject; public class SessionCounter implements HttpSessionListener { private static int activeSessions = 0; private static Vector v= new Vector(); public void sessionCreated(HttpSessionEvent se) { } public static void createsession(HttpSessionEvent se ) { UserObject user1 =(UserObject)se.getSession().getAttribute("UserObject"); if(v.contains(user1)==false){ v.add(user1); activeSessions++; System.out.println("===================用户名称=================="+user1.getName()); System.out.println("===================用户数量=================="+activeSessions); } } public void sessionDestroyed(HttpSessionEvent se) { System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"); UserObject user2=(UserObject)se.getSession().getAttribute("UserObject"); if(v.contains(user2)==true){ v.remove(user2); activeSessions--; System.out.println("===================removed=================="+user2.getName()); } } public static void Destroysession(UserObject user) { //System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"); //UserObject user3=(UserObject)se.getSession().getAttribute("UserObject"); System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"+user.getName()); if(v.contains(user)==true){ v.removeElement(user); activeSessions--; System.out.println("====================================="+user.getName()+"---退出登录!!!"); } } public static int getActiveSessions() { return activeSessions; } public static Vector getVector() { return v; } } |
===========================================================
在用户登陆后调用的
SessionCounter.createsession(new HttpSessionEvent(session)); SessionCounter.Destroysession(user); //注user 为用户实体 |
上一篇: ServletContextListener 应用
下一篇: HttpSessionListener