4、javaweb listener 应用场景

一、统计当前在线人数
   @Override
     public void sessionCreated(HttpSessionEvent se) {
         ServletContext context = se.getSession().getServletContext();
         Integer onLineCount = (Integer) context.getAttribute("onLineCount");
         if(onLineCount==null){
             context.setAttribute("onLineCount", );
         }else{
             onLineCount++;
             context.setAttribute("onLineCount", onLineCount);
         }
     }
 
     @Override
     public void sessionDestroyed(HttpSessionEvent se) {
         ServletContext context = se.getSession().getServletContext();
         Integer onLineCount = (Integer) context.getAttribute("onLineCount");
         if(onLineCount==null){
             context.setAttribute("onLineCount", );
         }else{
             onLineCount--;
             context.setAttribute("onLineCount", onLineCount);
         }
     }

你可能感兴趣的:(listener)