java監(jiān)聽器實現(xiàn)在線人數(shù)統(tǒng)計
本文實例為大家分享了java在線人數(shù)統(tǒng)計的具體代碼,供大家參考,具體內(nèi)容如下
1. 項目結(jié)構(gòu)
2. 代碼
package com; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; /** * Application Lifecycle Listener implementation class MyContexxtLis * */ @WebListener public class CountListen implements ServletContextListener { /** * Default constructor. */ public CountListen() { // TODO Auto-generated constructor stub } /** * @see ServletContextListener#contextInitialized(ServletContextEvent) */ public void contextInitialized(ServletContextEvent arg0) { arg0.getServletContext().setAttribute("count",100); } /** * @see ServletContextListener#contextDestroyed(ServletContextEvent) */ public void contextDestroyed(ServletContextEvent arg0) { // TODO Auto-generated method stub } }
package com; import javax.servlet.annotation.WebListener; import javax.servlet.http.HttpSessionAttributeListener; import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; @WebListener public class SessionListen implements HttpSessionListener, HttpSessionAttributeListener { public SessionListen() { // TODO Auto-generated constructor stub } public void attributeRemoved(HttpSessionBindingEvent arg0) { System.out.println("remove"+"\t"+arg0.getName()+arg0.getValue()); } public void attributeAdded(HttpSessionBindingEvent arg0) { System.out.println("add"+"\t"+arg0.getName()+arg0.getValue()); } public void attributeReplaced(HttpSessionBindingEvent arg0) { System.out.println("replace"+"\t"+arg0.getName()+arg0.getValue()); } public void sessionCreated(HttpSessionEvent arg0) { System.out.println("session create"); Integer i=(Integer)arg0.getSession().getServletContext().getAttribute("count"); i++; arg0.getSession().getServletContext().setAttribute("count", i); } public void sessionDestroyed(HttpSessionEvent arg0) { Integer i=(Integer)arg0.getSession().getServletContext().getAttribute("count"); i--; arg0.getSession().getServletContext().setAttribute("count", i); System.out.println("session destroy"+i); } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <%session.setMaxInactiveInterval(3); %> 當(dāng)前在線人數(shù):${count} </body> </html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot初始教程之Servlet、Filter、Listener配置詳解
本篇文章主要介紹了SpringBoot初始教程之Servlet、Filter、Listener配置詳解,具有一定的參考價值,有興趣的可以了解一下2017-09-09解讀CommandLineRunner和@PostConstruct區(qū)別與應(yīng)用場景
這篇文章主要介紹了解讀CommandLineRunner和@PostConstruct區(qū)別與應(yīng)用場景,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12springCloud gateWay 統(tǒng)一鑒權(quán)的實現(xiàn)代碼
這篇文章主要介紹了springCloud gateWay 統(tǒng)一鑒權(quán)的實現(xiàn)代碼,統(tǒng)一鑒權(quán)包括鑒權(quán)邏輯和代碼實現(xiàn),本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-02-02springboot使用DynamicDataSource動態(tài)切換數(shù)據(jù)源的實現(xiàn)過程
這篇文章主要給大家介紹了關(guān)于springboot使用DynamicDataSource動態(tài)切換數(shù)據(jù)源的實現(xiàn)過程,Spring Boot應(yīng)用中可以配置多個數(shù)據(jù)源,并根據(jù)注解靈活指定當(dāng)前使用的數(shù)據(jù)源,需要的朋友可以參考下2023-08-08SpringBoot使用MockMvc進(jìn)行單元測試的實例代碼
在Spring Boot應(yīng)用程序中,使用MockMvc進(jìn)行單元測試是一種有效的方式,可以驗證控制器的行為和API的正確性,在這篇博客中,我們將介紹如何使用MockMvc對用戶控制器進(jìn)行測試,感興趣的朋友可以參考下2024-01-01java實現(xiàn)服務(wù)器文件打包zip并下載的示例(邊打包邊下載)
這篇文章主要介紹了java實現(xiàn)服務(wù)器文件打包zip并下載的示例,使用該方法,可以即時打包文件,一邊打包一邊傳輸,不使用任何的緩存,讓用戶零等待,需要的朋友可以參考下2014-04-04