您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)怎么在java中使用監(jiān)聽器統(tǒng)計(jì)在線人數(shù),小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
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>
以上就是怎么在java中使用監(jiān)聽器統(tǒng)計(jì)在線人數(shù),小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。