欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

Javaweb实现在线人数统计代码实例

时间:2021-04-20 08:53:55|栏目:JAVA代码|点击:

这篇文章主要介绍了Javaweb实现在线人数统计代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

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%>" rel="external nofollow" >
  
  <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" rel="external nofollow" >
  -->

 </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();
   }

上一篇:Java反转数组输出实例代码

栏    目:JAVA代码

下一篇:Java开发中可以防止界面假死的刷新代码

本文标题:Javaweb实现在线人数统计代码实例

本文地址:http://www.codeinn.net/misctech/104713.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有