package com.oddel.listener;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class SessionListener implements HttpSessionListener {
public void sessionCreated(HttpSessionEvent event) {
// TODO Auto-generated method stub
HttpSession session=event.getSession();
SessionBean sessionBean=SessionFactory.getInstance().getSessionBean();
/**
* 访问次数统计
* */
sessionBean.setAccessCount(sessionBean.getAccessCount()+1);
/**
* 在线人数统计
* */
sessionBean.getOnline().put(session.getId(), null);
}
public void sessionDestroyed(HttpSessionEvent event) {
HttpSession session=event.getSession();
SessionBean sessionBean=SessionFactory.getInstance().getSessionBean();
sessionBean.getLogin().remove(sessionBean.getOnline().get(session.getId()));
sessionBean.getOnline().remove(session.getId());
}
}
package com.oddel.listener;
import javax.servlet.http.HttpSession;
public class SessionFactory {
private static SessionFactory factory;
private SessionBean sessionBean=new SessionBean();
private SessionFactory(){}
static{
factory=new SessionFactory();
}
public synchronized static SessionFactory getInstance() {
if (factory == null) {
factory = new SessionFactory();
}
return factory;
}
public void addLogin(HttpSession session, String user){
this.getSessionBean().getOnline().put(session.getId(), user);
this.getSessionBean().getLogin().add(user);
}
public void removeLogin(HttpSession session, String user){
this.getSessionBean().getLogin().remove(this.getSessionBean().getOnline().get(session.getId()));
this.getSessionBean().getOnline().remove(session.getId());
}
public boolean isOnline(String user){
return this.getSessionBean().getLogin().contains(user);
}
public SessionBean getSessionBean() {
return sessionBean;
}
public void setSessionBean(SessionBean sessionBean) {
this.sessionBean = sessionBean;
}
}
package com.oddel.listener;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class SessionBean {
private Map<String,String> online=new HashMap();//在线
private int accessCount=0;//系统访问人数
private Set<String> login=new HashSet();//已经登录用户
/**
* 返回当前系统在线人数
* @return int
*/
public int getOnlineCount(){
return this.getOnline().size();
}
/**
* 返回当前系统登录人数
* @return int
*/
public int getLoginCount(){
return this.getLogin().size();
}
public Map<String, String> getOnline() {
return online;
}
public void setOnline(Map<String, String> online) {
this.online = online;
}
public int getAccessCount() {
return accessCount;
}
public void setAccessCount(int accessCount) {
this.accessCount = accessCount;
}
public Set<String> getLogin() {
return login;
}
public void setLogin(Set<String> login) {
this.login = login;
}
}
Web.xml添加:
<listener>
<listener-class>
com.oddel.listener.SessionListener
</listener-class>
</listener>
用户登录时调用:
SessionFactory.getInstance().addLogin(httpSession, userid);
页面调用:
import="com.oddel.listener.SessionFactory;
SessionFactory.getInstance().isOnline(userid)
返回true表示用户当前是在线的。
返回false表示用户当前是不在线的。
http://www.xj-zp.com