网站的访问量和在线登录人数统计
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没有清除;这一步没有考虑