zoukankan      html  css  js  c++  java
  • 十三、监听器——网页在线人数

     1 package com.listener;
     2 
     3 import javax.servlet.ServletContext;
     4 import javax.servlet.ServletContextEvent;
     5 import javax.servlet.ServletContextListener;
     6 import javax.servlet.http.HttpSessionEvent;
     7 import javax.servlet.http.HttpSessionListener;
     8 
     9 public class MyListener implements HttpSessionListener,ServletContextListener{
    10     //监听Application对象
    11     @Override
    12     public void contextInitialized(ServletContextEvent sce) {
    13         int count=0;
    14         //获取Application对象
    15         ServletContext sc = sce.getServletContext();
    16         sc.setAttribute("count",count);
    17     }
    18 
    19     @Override
    20     public void contextDestroyed(ServletContextEvent sce) {
    21         // TODO Auto-generated method stub
    22 
    23     }
    24     //监听Sesion对象
    25     @Override
    26     public void sessionCreated(HttpSessionEvent se) {
    27         //获取Application对象中的计数器
    28         ServletContext sc = se.getSession().getServletContext();
    29         int count=(int) sc.getAttribute("count");
    30         //计数器自增
    31         ++count;
    32         //然后再将计数器存储到application中
    33         sc.setAttribute("count", count);
    34     }
    35 
    36     @Override
    37     public void sessionDestroyed(HttpSessionEvent se) {
    38         //获取Application对象中的计数器
    39         ServletContext sc = se.getSession().getServletContext();
    40         int count=(int) sc.getAttribute("count");
    41         //计数器自减
    42         --count;
    43         //然后再将计数器存储到application中
    44         sc.setAttribute("count", count);
    45 
    46     }
    47 
    48 }
    1     <listener>
    2         <listener-class>com.listener.MyListener</listener-class>
    3     </listener>
  • 相关阅读:
    一、cocos2d-x 3.0 final使用httpclient编译到android,须要用到的android.mk
    lvchange的available參数
    基于谱减法的声音去噪
    ios使用openUrl进行应用跳转
    linux下ssh免密登陆
    字体图标 icon font
    hdu 3642 Get The Treasury(扫描线)
    3D游戏引擎一 win32编程
    Codeforces 112B-Petya and Square(实现)
    动态规划 is beginning。。。。。。。。。
  • 原文地址:https://www.cnblogs.com/qiaoxin11/p/12950981.html
Copyright © 2011-2022 走看看