session 监听网站流量

用Session监听器,试下吧。将Session存活时间设短点。。。
一、实现Session监听器
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class OnlineCounter implements HttpSessionListener {

public synchronized void sessionCreated(HttpSessionEvent se){
  String count = (String)se.getSession().getServletContext().getAttribute("count");
  int c=0;
  if(count !=null){
    c = Integer.valueOf(count).intValue();
    System.out.println("旧Count:"+c);
    se.getSession().getServletContext().setAttribute("count",(c+1)+"");
  }else{
    System.out.println("新Count");
    se.getSession().getServletContext().setAttribute("count",c+1+"");
  }
  System.out.println("建立session时 Count:"+c);
}

public synchronized void sessionDestroyed(HttpSessionEvent se){
  System.out.println("session失效时 ");
  String count = (String)se.getSession().getServletContext().getAttribute("count");
  int c=0 ;
  if(count !=null){
    c = Integer.valueOf(count).intValue();
    if(c>=1){
    System.out.println("sessionDestroyed:旧c-1:"+c);
    se.getSession().getServletContext().setAttribute("count",(c-1)+"");
    }
  }else{
    System.out.println("sessionDestroyed:旧新"+c);
    se.getSession().getServletContext().setAttribute("count","1");
  }
  // session失效时
}
}


二、配置Session监听器 与Session存活时间

<listener>
        <listener-class>
            com.longqiao.tools.servlet.OnlineCounter
        </listener-class>
    </listener>
<session-config>
    <session-timeout>1 </session-timeout> <!-- Session存活时间为1分钟 -->
</session-config>

三、从前台提出网站在线人数...

当前在线人数:${applicationScope.count}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值