JSP&Servlet 统计在线人数及信息

首先创建一个javabean的user类:
包括sessionIdString,ipString,firstTimeString属性。
创建session监听:
    @WebListener
    public class MyHttpSessionListener implements HttpSessionListener {
        private int userNumber = 0;

        @Override
        public void sessionCreated(HttpSessionEvent arg0) {
            userNumber++;
            arg0.getSession().getServletContext().setAttribute("userNumber", userNumber);
        }

        @Override
        public void sessionDestroyed(HttpSessionEvent arg0) {
            userNumber--;
            arg0.getSession().getServletContext().setAttribute("userNumber", userNumber);
            ArrayList<User> userList = null;//在线用户List
            userList = (ArrayList<User>) arg0.getSession().getServletContext().getAttribute("userList");
            if (SessionUtil.getUserBySessionId(userList, arg0.getSession().getId()) != null) {
                userList.remove(SessionUtil.getUserBySessionId(userList, arg0.getSession().getId()));
            }
        }
    }

创建一个请求监听:
    @WebListener
    public class MyServletRequestListener implements ServletRequestListener {
        private ArrayList<User> userList;//在线用户List

        @Override
        public void requestDestroyed(ServletRequestEvent arg0) {
        }

        @Override
        public void requestInitialized(ServletRequestEvent arg0) {
            userList = (ArrayList<User>) arg0.getServletContext().getAttribute("userList");
            if (userList == null)
                userList = new ArrayList<User>();
            HttpServletRequest request = (HttpServletRequest) arg0.getServletRequest();
            String sessionIdString = request.getSession().getId();
            if (SessionUtil.getUserBySessionId(userList, sessionIdString) == null) {
                User user = new User();
                user.setSessionIdString(sessionIdString);
                user.setFirstTimeString(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                user.setIpString(request.getRemoteAddr());
                userList.add(user);
            }
            arg0.getServletContext().setAttribute("userList", userList);
        }
    }

实现请求监听中的getUserBySessionId方法,创建单独的类SessionUtil实现:

    public class SessionUtil {
        public static Object getUserBySessionId(ArrayList<User> userList, String sessionIdString) {
            for (int i = 0; i < userList.size(); i++) {
                User user = userList.get(i);
                if (user.getSessionIdString().equals(sessionIdString)) {
                    return user;
                }
            }
            return null;
        }
    }

修改index.jsp:
<body>
    当前在线用户人数:${userNumber }<br/>
    <% 
   ArrayList<com.imooc.entity.User>  userList =  (ArrayList<com.imooc.entity.User>)request.getServletContext().getAttribute("userList"); 
   if(userList!=null){
       for(int i = 0 ; i < userList.size() ; i++){
      com.imooc.entity.User user = userList.get(i);
   %>
    IP:<%=user.getIpString() %>,FirstTime:<%=user.getFirstTimeString() %>,SessionId:<%=user.getSessionIdString() %> <br/>
    <%}} %>
  </body>

打开一个浏览器,进入index.jsp:
网页显示:
当前在线用户人数:1
IP:0:0:0:0:0:0:0:1,FirstTime:2015-09-09 21:32:38,SessionId:707E948E950F1A4AB963275EC5C1055B
打开另一不同的浏览器
显示:
当前在线用户人数:2
 IP:0:0:0:0:0:0:0:1,FirstTime:2015-09-09 21:32:38,SessionId:707E948E950F1A4AB963275EC5C1055B 
 IP:0:0:0:0:0:0:0:1,FirstTime:2015-09-09 21:33:46,SessionId:8B11D420FB1166266745510293E1B05D 
刷新第一个,显示第二个相同。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值