如何编写Java Web在线用户数量统计的示例代码?
- 内容介绍
- 文章标签
- 相关推荐
本文共计453个文字,预计阅读时间需要2分钟。
本文简要介绍了JavaWeb实现在线人数统计的代码实例,内容中通过示例代码进行说明,但描述较为简略。对于想要深入学习或工作的朋友,具有一定的参考价值。需要的朋友可以参考以下示例代码:session。并非浏览器关闭。
这篇文章主要介绍了Javaweb实现在线人数统计代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
session并不是浏览器关闭时销毁的,而是在session失效的时候销毁下列代码就是监测session创建、销毁
package com.my.count; import javax.servlet."+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(); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。
本文共计453个文字,预计阅读时间需要2分钟。
本文简要介绍了JavaWeb实现在线人数统计的代码实例,内容中通过示例代码进行说明,但描述较为简略。对于想要深入学习或工作的朋友,具有一定的参考价值。需要的朋友可以参考以下示例代码:session。并非浏览器关闭。
这篇文章主要介绍了Javaweb实现在线人数统计代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
session并不是浏览器关闭时销毁的,而是在session失效的时候销毁下列代码就是监测session创建、销毁
package com.my.count; import javax.servlet."+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(); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

