Javaweb實現(xiàn)在線人數(shù)統(tǒng)計代碼實例
這篇文章主要介紹了Javaweb實現(xiàn)在線人數(shù)統(tǒng)計代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
session并不是瀏覽器關(guān)閉時銷毀的,而是在session失效的時候銷毀下列代碼就是監(jiān)測session創(chuàng)建、銷毀
package com.my.count; import javax.servlet.http.*; public class SessionCounter implements HttpSessionListener { private static int activeSessions = 0; //session創(chuàng)建時執(zhí)行 public void sessionCreated(HttpSessionEvent se) { activeSessions++; } //session銷毀時執(zhí)行 public void sessionDestroyed(HttpSessionEvent se) { if (activeSessions > 0) activeSessions--; } //獲取活動的session個數(shù)(在線人數(shù)) 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> 在線人數(shù)為:<%=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創(chuàng)建時執(zhí)行 public void sessionCreated(HttpSessionEvent se) { activeSessions++; } //session銷毀時執(zhí)行 public void sessionDestroyed(HttpSessionEvent se) { if (activeSessions > 0) activeSessions--; } //獲取活動的session個數(shù)(在線人數(shù)) public static int getActiveSessions() { return activeSessions; } } /** * 在線用戶人數(shù) * @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(); }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java dom4j創(chuàng)建解析xml文檔過程解析
這篇文章主要介紹了Java dom4j創(chuàng)建解析xml文檔過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07SpringMVC數(shù)據(jù)響應(yīng)詳細(xì)介紹
Spring MVC 是 Spring 提供的一個基于 MVC 設(shè)計模式的輕量級 Web 開發(fā)框架,本質(zhì)上相當(dāng)于 Servlet,Spring MVC 角色劃分清晰,分工明細(xì),本章來講解SpringMVC數(shù)據(jù)響應(yīng)2023-02-02基于ScheduledExecutorService的兩種方法(詳解)
下面小編就為大家?guī)硪黄赟cheduledExecutorService的兩種方法(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10Spring注解@RestControllerAdvice原理解析
這篇文章主要介紹了Spring注解@RestControllerAdvice原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11java如何更改數(shù)據(jù)庫中的數(shù)據(jù)
這篇文章主要介紹了java如何更改數(shù)據(jù)庫中的數(shù)據(jù),修改數(shù)據(jù)庫是數(shù)據(jù)庫操作必不可少的一部分,使用Statement接口中的excuteUpdate()方法可以修改數(shù)據(jù)表中的數(shù)據(jù),感興趣的朋友跟隨小編一起看看吧2021-11-11深度解析Java中volatile的內(nèi)存語義實現(xiàn)以及運用場景
這篇文章主要介紹了Java中volatile的內(nèi)存語義實現(xiàn)以及運用場景,通過JVM的機制來分析volatile關(guān)鍵字在線程編程中的作用,需要的朋友可以參考下2015-12-12