首页 > 编程语言 > 详细

javaweb中实现在线人数统计

时间:2019-11-21 12:52:21      阅读:92      评论:0      收藏:0      [点我收藏+]

session并不是浏览器关闭时销毁的,而是在session失效的时候销毁下列代码就是监测session创建、销毁

 

package com.my.count;

import javax.servlet.http.*;

public class SessionCounter implements HttpSessionListener {

    private static int activeSessions = 0;
    //session创建时执行
    public void sessionCreated(HttpSessionEvent se) {
        activeSessions++;
    }
    //session销毁时执行
    public void sessionDestroyed(HttpSessionEvent se) {
        if (activeSessions > 0)
            activeSessions--;
    }
    //获取活动的session个数(在线人数)
    public static int getActiveSessions() {
        return activeSessions;
    }

}
技术分享图片

 

接下来就是配置web.xml

<listener>
      <listener-class>
          com.my.count.SessionCounter  //这里是包名加类名
      </listener-class>
  </listener>
技术分享图片

 

接下来就可以在jsp页面中使用

 

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="com.my.count.SessionCounter"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP ‘ApplicationTest.jsp‘ starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
        在线人数为:<%=SessionCounter.getActiveSessions() %>
  </body>
</html>
技术分享图片

不用jsp页面 写成接口代码如下:

 

package com.wangyun.web.controllers;

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class SessionCounter implements HttpSessionListener {

     static int activeSessions = 0;
    //session创建时执行
    public void sessionCreated(HttpSessionEvent se) {
        activeSessions++;
    }
    //session销毁时执行
    public void sessionDestroyed(HttpSessionEvent se) {
        if (activeSessions > 0)
            activeSessions--;
    }
    //获取活动的session个数(在线人数)
    public static int getActiveSessions() {
        return activeSessions;
    }

}
技术分享图片
   /**
  	 * 在线用户人数
  	 * @throws IOException 
  	 * @throws ServletException 
  	 */
      @RequestMapping(value="user_online", method=RequestMethod.POST, produces="text/json;charset=utf-8")
  	@ResponseBody
  	public Object user_online() throws ServletException, IOException {
  			JSONObject data = new JSONObject();
  			int number=SessionCounter.activeSessions;
  			data.put("msg",number);
  			return data.toString();
      }
技术分享图片

 

javaweb中实现在线人数统计

原文:https://www.cnblogs.com/dreamrobot/p/11904818.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!