java監(jiān)聽器實(shí)現(xiàn)在線人數(shù)統(tǒng)計(jì)
本文實(shí)例為大家分享了java在線人數(shù)統(tǒng)計(jì)的具體代碼,供大家參考,具體內(nèi)容如下
1. 項(xiàng)目結(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 X-Accel-Redirect 大文件下載實(shí)現(xiàn)
本文主要介紹了springboot X-Accel-Redirect 大文件下載實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
springboot讀取.properties配置文件中的map和list類型配置參數(shù)方式
這篇文章主要介紹了springboot讀取.properties配置文件中的map和list類型配置參數(shù)方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03
Java實(shí)現(xiàn)解析zip壓縮包并獲取文件內(nèi)容
這篇文章主要為大家詳細(xì)介紹了如何利用Java語言實(shí)現(xiàn)頁面上傳一個(gè)源碼壓縮包,后端將壓縮包解壓,并獲取每個(gè)文件中的內(nèi)容,感興趣的可以動(dòng)手嘗試一下2022-07-07
Java實(shí)現(xiàn)狀態(tài)模式的示例代碼
狀態(tài)模式是一種行為型設(shè)計(jì)模式,允許對象根據(jù)其內(nèi)部狀態(tài)改變行為,本文主要介紹了Java實(shí)現(xiàn)狀態(tài)模式的示例代碼,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-02-02
使用Java實(shí)現(xiàn)MySQL數(shù)據(jù)鎖定的策略
在并發(fā)環(huán)境下,多個(gè)線程同時(shí)對MySQL數(shù)據(jù)庫進(jìn)行讀寫操作可能會(huì)導(dǎo)致數(shù)據(jù)沖突和不一致的問題,為了解決這些并發(fā)沖突,我們可以采用數(shù)據(jù)鎖定策略來保證數(shù)據(jù)的一致性和完整性,下面將介紹如何使用Java實(shí)現(xiàn)MySQL數(shù)據(jù)鎖定策略,,需要的朋友可以參考下2023-08-08
java使用mysql預(yù)編譯語句查詢優(yōu)勢及示例詳解
這篇文章主要為大家介紹了java使用mysql預(yù)編譯語句的優(yōu)勢特點(diǎn)及示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06

