介绍
本篇文章给大家分享的是有关怎么在java中使用监听器统计在线人数,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
1。项目结构
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 构造函数只 ,,*/public 才能;CountListen (), { ,,,//,TODO Auto-generated constructor 存根 ,,} ,/* * ,,*,@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 存根 ,,} , }
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 存根 ,,} , 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=(整数)arg0.getSession () .getServletContext () .getAttribute (“count"); ,我+ +; .getServletContext, arg0.getSession () () .setAttribute (“count",, i); , ,,} , public 才能;void  sessionDestroyed (HttpSessionEvent arg0), {, ,Integer i=(整数)arg0.getSession () .getServletContext () .getAttribute (“count"); ,我,; .getServletContext, arg0.getSession () () .setAttribute (“count",, i); ,System.out.println (“session destroy" + i); , ,,} , }
& lt; % @ page 语言=癹ava", contentType=皌ext/html;, charset=UTF-8" pageEncoding才能=癠TF-8" %比; & lt; ! DOCTYPE html  PUBLIC “-//W3C//DTD html 4.01,过渡//EN",“http://www.w3.org/TR/html4/loose.dtd"比; & lt; html> & lt; head> & lt; meta  http-equiv=癈ontent-Type",内容=皌ext/html;, charset=UTF-8"比; & lt; title> Insert title  here & lt;/head> & lt; body> & lt; % session.setMaxInactiveInterval(3);, %比; 当前在线人数:${数} & lt;/body> & lt;/html>
以上就是怎么在java中使用监听器统计在线人数,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注行业资讯频道。