给你一段代码,我自己以前写的,你可以参考一下,注释可能做的不是很好,将就一下吧
import java.util.ArrayList ;
import java.util.HashSet ;
import java.util.List ;
import java.util.Set ;
import javax.servlet.ServletContext ;
import javax.servlet.ServletContextEvent ;
import javax.servlet.ServletContextListener ;
import javax.servlet.http.HttpSession ;
import javax.servlet.http.HttpSessionAttributeListener ;
import javax.servlet.http.HttpSessionBindingEvent ;
import javax.servlet.http.HttpSessionEvent ;
import javax.servlet.http.HttpSessionListener ;
import cn.edu.fjnu.cse.po.User ;
/**
* @author puderty
*/
public class SessionListener implements HttpSessionListener , ServletContextListener , HttpSessionAttributeListener
{
// 获取application对象
ServletContext application = null ;
HttpSession session = null ;
Set
int count, count1, count2 = 0 ;
public void sessionCreated( HttpSessionEvent arg0 )
{
System.out.println("新的会话:") ;
count = (Integer) application.getAttribute("anonyUserCount") ;
count++ ;
application.setAttribute("anonyUserCount", count) ;
count1 = (Integer) application.getAttribute("anonyUserCount") ;
count2 = (Integer) application.getAttribute("registerUserCount") ;
count = count1 + count2 ;
application.setAttribute("allUserCount", count) ;
}
public void sessionDestroyed( HttpSessionEvent arg0 )
{
session = arg0.getSession() ;
if ( session.getAttribute("customer") != null )
{
System.out.println("用户注销....") ;
User u = (User) session.getAttribute("customer") ;
String userid = u.getUserid() ;
set = (Set) application.getAttribute("registerUser") ;
set.remove(userid) ;
application.setAttribute("registerUser", set) ;
count = (Integer) application.getAttribute("registerUserCount") ;
if ( count > 0 )
{
count-- ;
}
application.setAttribute("registerUserCount", count) ;
count1 = (Integer) application.getAttribute("anonyUserCount") ;
count2 = (Integer) application.getAttribute("registerUserCount") ;
count = count1 + count2 ;
application.setAttribute("allUserCount", count) ;
}
else
{
System.out.println("用户超时....") ;
count = (Integer) application.getAttribute("anonyUserCount") ;
if ( count > 0 )
{
count-- ;
}
application.setAttribute("anonyUserCount", count) ;
}
count1 = (Integer) application.getAttribute("anonyUserCount") ;
count2 = (Integer) application.getAttribute("registerUserCount") ;
count = count1 + count2 ;
application.setAttribute("allUserCount", count) ;
}
public void attributeAdded( HttpSessionBindingEvent arg0 )
{
session = arg0.getSession() ;
if ( session.getAttribute("customer") != null )
{
session = arg0.getSession() ;
User u = (User) session.getAttribute("customer") ;
String userid = u.getUserid() ;
set = (Set) application.getAttribute("registerUser") ;
// 新用户登陆
if ( !set.contains(userid) )
{
set.add(userid) ;
count = (Integer) application.getAttribute("registerUserCount") ;
count++ ;
application.setAttribute("registerUserCount", count) ;
count = (Integer) application.getAttribute("anonyUserCount") ;
if ( count > 0 )
{
count-- ;
}
;
application.setAttribute("anonyUserCount", count) ;
}
}
count1 = (Integer) application.getAttribute("anonyUserCount") ;
count2 = (Integer) application.getAttribute("registerUserCount") ;
count = count1 + count2 ;
application.setAttribute("allUserCount", count) ;
}
public void attributeRemoved( HttpSessionBindingEvent arg0 )
{
// TODO Auto-generated method stub
}
public void attributeReplaced( HttpSessionBindingEvent arg0 )
{
// TODO Auto-generated method stub
}
public void contextDestroyed( ServletContextEvent arg0 )
{
// TODO Auto-generated method stub
}
public void contextInitialized( ServletContextEvent arg0 )
{
// tomcat启动时创建用于保存人数的application对象
application = arg0.getServletContext() ;
application.setAttribute("allUserCount", new Integer(0)) ;// 总在线人数
application.setAttribute("registerUser", new HashSet
application.setAttribute("registerUserCount", new Integer(0)) ;// 已注册用户数
application.setAttribute("anonyUserCount", new Integer(0)) ;// 匿名用户数
}
}
用SessionListener 来监听Session的创建和销毁,
你要统计在线人数是登录用户的人数,还是游客的人数,还是所有人数?
Session监听就是任何一个人登录到网站上之后服务器会创建一个Session,因此一旦监听到创建一个Session就是来了一个人,这个人30分钟不活动,或者离开后30分钟,session就会销毁。这时人数减1即可。
要用户刚一离开就人数减一是很难实现的,要用比较复杂的javascript或ajax
统计Session的创建数目!
设计数据库表的问题了或者用服务器端的session