如何通过监听器统计在线人数?
生活随笔
收集整理的這篇文章主要介紹了
如何通过监听器统计在线人数?
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
public class HellServlet implements HttpSessionListener{@Overridepublic void sessionCreated(HttpSessionEvent arg0) {// TODO Auto-generated method stubSystem.out.println("HttpSession被建立");ServletContext application=arg0.getSession().getServletContext();Integer count = (Integer)application.getAttribute("count");if(count==null){count=1;application.setAttribute("count", count);}else{count++;application.setAttribute("count", count);}}@Overridepublic void sessionDestroyed(HttpSessionEvent arg0) {// TODO Auto-generated method stubSystem.out.println("HttpSession被銷毀");ServletContext application=arg0.getSession().getServletContext();Integer count = (Integer)application.getAttribute("count");count--;application.setAttribute("count", count);}}//配置web.xml<listener><listener-class>com.kgc.listener.HellServlet </listener-class> </listener>
jsp 頁面
<%Integer count=(Integer)application.getAttribute("count");out.println(“當前在線人數:”+count);%> <--下面等價getAttribute()--> 當前在線用戶人數:${applicationScope.count}人
<%Integer count=(Integer)application.getAttribute("count");out.println(“當前在線人數:”+count);%> <--下面等價getAttribute()--> 當前在線用戶人數:${applicationScope.count}人
總結
以上是生活随笔為你收集整理的如何通过监听器统计在线人数?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python周报第十五周
- 下一篇: 易语言多线程教程