网站访问流量统计

//test.Online
public class Online implements HttpSessionListener{


public void sessionCreated(HttpSessionEvent arg0) {
OnLineCount.raise();
}

public void sessionDestroyed(HttpSessionEvent arg0) {
OnLineCount.reduce();
}

}

public class OnLineCount {

public static long onLineCount = 0;
public static long currentOnLineCount = 0;
public static OnLineCount oc =null;

public synchronized static void raise(){
onLineCount++;
currentOnLineCount++;
}
public synchronized static void reduce(){
currentOnLineCount--;
}

public synchronized long getCurrentOnLineCount(){
return currentOnLineCount;
}

public static synchronized OnLineCount getInstance(){
if(oc == null)
oc = new OnLineCount();
return oc;
}

}

//web.xml中加入监听
<listener>
  <description>HttpSessionListener</description>
  <listener-class>test.Online</listener-class>
</listener>
//web.xml中设置session过期时间 单位为 分钟
  <session-config>
        <session-timeout>3</session-timeout>
</session-config>

test.java
public class Test{
public static void mian(String args[]){
  System.out.println("历史总访问人数:"+OnLineCount.onLineCount);
   System.out.println("当前在线人数:"+OnLineCount.currentOnLineCount);
}
}

//当然了这个统计的流量肯定是有误差的了

你可能感兴趣的:(Web,xml)