您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關java中怎么利用監聽器統計在線人數,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
package com; import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;import javax.servlet.annotation.WebListener; /** * Application Lifecycle Listener implementation class MyContexxtLis * */@WebListenerpublic 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; @WebListenerpublic 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); %>當前在線人數:${count}</body></html>
關于java中怎么利用監聽器統計在線人數就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。