使用HttpSessionListener監(jiān)聽(tīng)器實(shí)戰(zhàn)
HttpSessionListener監(jiān)聽(tīng)器
定義監(jiān)聽(tīng)器
package lee; import javax.servlet.*; import javax.servlet.annotation.*; import javax.servlet.http.*;import java.util.*; @WebListener public class OnlineListener implements HttpSessionListener { // 當(dāng)用戶(hù)與服務(wù)器之間開(kāi)始session時(shí)觸發(fā)該方法 public void sessionCreated(HttpSessionEvent se) { HttpSession session = se.getSession(); ServletContext application = session.getServletContext(); // 獲取session ID String sessionId = session.getId(); // 如果是一次新的會(huì)話 if (session.isNew()) { String user = (String)session.getAttribute("user"); // 未登錄用戶(hù)當(dāng)游客處理 user = (user == null) ? "游客" : user; Map<String , String> online = (Map<String , String>) application.getAttribute("online"); if (online == null) { online = new Hashtable<String , String>(); } // 將用戶(hù)在線信息放入Map中 online.put(sessionId , user); application.setAttribute("online" , online); } } // 當(dāng)用戶(hù)與服務(wù)器之間session斷開(kāi)時(shí)觸發(fā)該方法 public void sessionDestroyed(HttpSessionEvent se) { HttpSession session = se.getSession(); ServletContext application = session.getServletContext(); String sessionId = session.getId(); Map<String , String> online = (Map<String , String>) application.getAttribute("online"); if (online != null) { // 刪除該用戶(hù)的在線信息 online.remove(sessionId); } application.setAttribute("online" , online); } }
測(cè)試JSP
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %> <%@ page import="java.util.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> 用戶(hù)在線信息 </title> <meta name="website" content="http://www.crazyit.org" /> </head> <body> 在線用戶(hù): <table width="400" border="1"> <% Map<String , String> online = (Map<String , String>)application .getAttribute("online"); for (String sessionId : online.keySet()) {%> <tr> <td><%=sessionId%> <td><%=online.get(sessionId)%> </tr> <%}%> </body> </html>
測(cè)試結(jié)果
HttpSessionListener監(jiān)聽(tīng)器應(yīng)用場(chǎng)景
應(yīng)用場(chǎng)景:用來(lái)統(tǒng)計(jì)當(dāng)前在線人數(shù)
實(shí)現(xiàn)HttpSessionListener
import javax.servlet.ServletContext; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; public class MyHttpSessionListener implements HttpSessionListener { @Override public void sessionCreated(HttpSessionEvent httpSessionEvent) { System.out.println("httpsession被創(chuàng)建"); } @Override public void sessionDestroyed(HttpSessionEvent httpSessionEvent) { System.out.println("httpsession被銷(xiāo)毀"); } }
登陸界面去創(chuàng)建HttpSessionListenter
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>$Title$</title> </head> <body> <% <%-- 創(chuàng)建HttpSessionListenter--%> request.getSession(); %> </body> </html>
登出銷(xiāo)毀HttpSessionListenter
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>$Title$</title> </head> <body> <% <%-- 銷(xiāo)毀HttpSessionListener--%> request.getSession().invalidate(); %> <h1>已退出</h1> </body> </html>
實(shí)現(xiàn)統(tǒng)計(jì)登陸人數(shù)(多線程并發(fā))
web.xml中配置監(jiān)聽(tīng)
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <listener> <listener-class>MyHttpSessionListener</listener-class> </listener> <listener> <listener-class>myServletContextListener</listener-class> </listener> </web-app>
統(tǒng)計(jì)人數(shù)實(shí)在最大ServletContextListener這個(gè)域當(dāng)中
因?yàn)镠ttpSessionListener監(jiān)聽(tīng)器只在當(dāng)前會(huì)話中有效
(1)創(chuàng)建ServletContextListener監(jiān)聽(tīng)器并設(shè)置初始值為0
import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class myServletContextListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent servletContextEvent) { ServletContext sc = servletContextEvent.getServletContext(); sc.setAttribute("count", 0); } @Override public void contextDestroyed(ServletContextEvent servletContextEvent) { } }
2)變更在線人數(shù)
import javax.servlet.ServletContext; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; public class MyHttpSessionListener implements HttpSessionListener { @Override public void sessionCreated(HttpSessionEvent httpSessionEvent) { System.out.println("httpsession被創(chuàng)建"); countPersion( httpSessionEvent.getSession().getServletContext(), true); } @Override public void sessionDestroyed(HttpSessionEvent httpSessionEvent) { System.out.println("httpsession被銷(xiāo)毀"); countPersion(httpSessionEvent.getSession().getServletContext(), false); } /* * 變更在線的人數(shù) * */ public void countPersion(ServletContext sc, boolean isAdd) { // 為了防止多線程并發(fā)問(wèn)題加鎖 synchronized (sc) { // 獲得當(dāng)前的在線人數(shù) Integer count = (Integer) sc.getAttribute("count"); if(isAdd) { sc.setAttribute("count", ++count); } else { sc.setAttribute("count", --count); } } } }
(3)前端頁(yè)面上去獲取顯示
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>$Title$</title> </head> <body> <% <%-- 創(chuàng)建HttpSessionListenter--%> request.getSession(); %> <h1>歡迎登陸</h1> <hr> 當(dāng)前的在線人數(shù) ${count} <a href="logout.jsp" rel="external nofollow" >退出</a> </body> </html>
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
淺談PrintStream和PrintWriter的區(qū)別和聯(lián)系
這篇文章主要介紹了淺談PrintStream和PrintWriter的區(qū)別和聯(lián)系,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03Spring事物基礎(chǔ)知識(shí)及AOP相關(guān)陷阱分析
這篇文章主要介紹了Spring事物基礎(chǔ)知識(shí)及AOP相關(guān)陷阱,在平時(shí)的實(shí)際開(kāi)發(fā)中經(jīng)常會(huì)遇到,只有深入了解了其中的原理,才會(huì)在工作中能夠有效應(yīng)對(duì)2021-09-09Java Collections.EMPTY_LIST與Collections.emptyList()的區(qū)別
這篇文章主要介紹了Java Collections.EMPTY_LIST與Collections.emptyList()的區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11如何基于springboot-admin實(shí)現(xiàn)后臺(tái)監(jiān)控
這篇文章主要介紹了如何基于springboot-admin實(shí)現(xiàn)后臺(tái)監(jiān)控,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04Java如何設(shè)置系統(tǒng)參數(shù)和運(yùn)行參數(shù)
這篇文章主要介紹了Java如何設(shè)置系統(tǒng)參數(shù)和運(yùn)行參數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04java報(bào)錯(cuò)之springboot3+vue2項(xiàng)目web服務(wù)層報(bào)錯(cuò)總結(jié)
java入門(mén)學(xué)習(xí),隨手記錄一下開(kāi)發(fā)過(guò)程中產(chǎn)生的報(bào)錯(cuò),有些錯(cuò)誤是網(wǎng)上搜索再加上自己嘗試,隨手引用了一些其他人的記錄,也是留給自己看的,或是希望能對(duì)其他初學(xué)者有幫助2023-06-06