访问量和在线人数统计

网站的访问量和在线登录人数统计


web.xml配置:

  <listener>
  <listener-class>
  com.dept.web.general.interceptor.UserListener
  </listener-class>
 </listener>  
  <listener>
  <listener-class>
  com.dept.web.general.interceptor.OnlineSessionListener
  </listener-class>
 </listener>  

在线人数:

</pre><pre name="code" class="java">import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class OnlineSessionListener implements HttpSessionListener {
	private OnlineList ol = OnlineList.getInstance();

	public void sessionCreated(HttpSessionEvent arg0) {
	ol.addSession();
	}

	public void sessionDestroyed(HttpSessionEvent arg0) {
	ol.delSession();
	}
}


</pre><pre name="code" class="java">package com.web.interceptor;

public class OnlineList {
	private static final OnlineList onlineList = new OnlineList();
	private int maxSession;
	private int activeSession;

	private OnlineList() {
		// v = new Vector();
	}

	public static OnlineList getInstance() {
		return onlineList;
	}

	public void addSession() {
	activeSession++;
	
		if (activeSession >= maxSession) {
			maxSession = activeSession;
		}
	}

	public void delSession() {
	if (activeSession > 0)
		activeSession--;
	}

	public int getActiveSession() {
		return activeSession;
	}

	public int getmaxSession() {
		return maxSession;
	}
}

------------------------------------------------------------------------------------

访问量统计:

import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;


public class UserListener implements HttpSessionAttributeListener {
	// 用户登录身份证
	// private String USERNAME;
	private UserList u1 = UserList.getInstance();

	// 判断用户是否存在
	public boolean IsExist(String sfz) throws Exception {
		try {
			return u1.IsExist(sfz);
		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}
	}
	//没搞懂这是什么意思;有什么用
	// public String getUSERNAME() {
		// return USERNAME;
	// }

	// public void setUSERNAME(String username) {
		// USERNAME = username;
	// }

	public void attributeAdded(HttpSessionBindingEvent event) {
		try {
			//来自于定义的session的名字
			if ("USER_ID".equals(event.getName())) {
				if (event.getValue() != null) {
					u1.addUser(event.getValue().toString());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void attributeRemoved(HttpSessionBindingEvent event) {
		try {
			if ("USER_ID".equals(event.getName())) {
				if (event.getValue() != null) {
					u1.RemoveUser(event.getValue().toString());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void attributeReplaced(HttpSessionBindingEvent arg0) {
		// TODO Auto-generated method stub
	}
}


package com.web.interceptor;

public class OnlineList {
	private static final OnlineList onlineList = new OnlineList();
	private int maxSession;
	private int activeSession;

	private OnlineList() {
		// v = new Vector();
	}

	public static OnlineList getInstance() {
		return onlineList;
	}

	public void addSession() {
	activeSession++;
	
		if (activeSession >= maxSession) {
			maxSession = activeSession;
		}
	}

	public void delSession() {
	if (activeSession > 0)
		activeSession--;
	}

	public int getActiveSession() {
		return activeSession;
	}

	public int getmaxSession() {
		return maxSession;
	}
}
直接关闭游览器造成的服务端session没有清除;这一步没有考虑


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值