日常笔记——关于在线人数统计问题

SessionCounter.java

package com.my.count;

import javax.servlet.ServletContext;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.springframework.context.ApplicationContext;


/**
 * Application Lifecycle Listener implementation class SessionCounter
 *
 */
@WebListener
public class SessionCounter implements HttpSessionListener { 
        //total为访问总人数,onLineUser为在线人数
        private static int onLineUser=0;
        public static int total=82553;

        public SessionCounter() {
            // TODO Auto-generated constructor stub
        }
        @Override
        public void sessionCreated(HttpSessionEvent event)  
        {  
            System.out.println("sessionCreated");
            ServletContext sct = event.getSession().getServletContext();

            Integer onLineUser = (Integer) sct.getAttribute("onLineUser");  
            if (onLineUser== null) {  
                onLineUser = new Integer(1);  
            }else {  
                int count = onLineUser.intValue();  
                onLineUser = new Integer(count+1);  
            }  

            /*Integer total = (Integer) sct.getAttribute("total"); 
            int countTotal = total.intValue();  
            total = new Integer(countTotal+1);*/
            //Integer total = (Integer) sct.getAttribute("total");
            sct.setAttribute("onLineUser", onLineUser);
            sct.setAttribute("total", total++);

        }  
        @Override
        public void sessionDestroyed(HttpSessionEvent event){
            System.out.println("sessionDestroyed");
            ServletContext sct = event.getSession().getServletContext();  
            Integer onLineUser = (Integer) sct.getAttribute("onLineUser");  
            if (onLineUser == null) {  
                onLineUser = new Integer(0);  
            }else {  
                int count = onLineUser.intValue();  
                onLineUser = new Integer(count-1);  
                System.out.println("----------------------"+onLineUser);
            }  
           // Integer total = (Integer) sct.getAttribute("total");
            sct.setAttribute("onLineUser", onLineUser);  
            sct.setAttribute("total", total); 
            //销毁session  
            // HttpSession session = event.getSession();  
            // session.invalidate(); 

        } 
        public static int getCount() {
            return onLineUser;
        }

        public static void setCount(int onLineUser) {
            SessionCounter.onLineUser =onLineUser;
        }

    }  

配置web.xml(要写在配置文件较前位置,先执行,此处写在SpringMvc的Listerner之前)
`
<listener>
<listener-class>
com.my.count.SessionCounter
</listener-class>
</listener>
///
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<session-config>
<session-timeout>1</session-timeout>
</session-config>
//销毁session时间配置------一分钟

销毁session时间配置——一分钟

总结:sessionDestroyed()不执行的原因
1、listener在web.xml的配置文件中放置位置不正确
2、销毁时间过长,需设置销毁时间

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值