这个类倒和你所说的差不多阿!import javax.servlet.*; import javax.servlet.http.*;public final class CounterListener implements HttpSessionListener { private int count = 10; private ServletContext context = null; public synchronized void sessionCreated(HttpSessionEvent se) { count++; log("sessionCreated('" + se.getSession().getId() + "'LuoTing's Log)"+" count="+count); se.getSession().setAttribute("count",new Integer(count)); } public synchronized void sessionDestroyed(HttpSessionEvent se) { count--; se.getSession().setAttribute("count",new Integer(count)); } public int getCount() { return this.count; } public void addCount(){ count++; } private void log(String message) { if (context != null) context.log("SessionListener: " + message); else System.out.println("SessionListener: " + message); } }
public class SessionListener implements HttpSessionListener { public synchronized void sessionCreated(HttpSessionEvent se) { se.getSession().setAttribute("time",System.currentTimeMillis()+""); } public synchronized void sessionDestroyed(HttpSessionEvent se) { long startTime=Long.parseLong(se.getSession().getAttribute("time")); long endTime=System.currentTimeMillis(); long time=(endTime-startTime)/1000; System.out.println("此用户在线时间为:"+time+"秒"); } }
你是想得到一个人的在线时间? 上面统计的是每个人单个的在线时间,要在JSP页面上访问到,就把他存到application里就可以得到了 public synchronized void sessionDestroyed(HttpSessionEvent se) { long startTime=Long.parseLong(se.getSession().getAttribute("time")); long endTime=System.currentTimeMillis(); long time=(endTime-startTime)/1000; System.out.println("此用户在线时间为:"+time+"秒"); se.getSession().getServletContext().setAttribute("time",time+""); }在JSP页面上可以通过application.getAttribute("time");得到刚刚离开的用户在线时间
public class TimeBean{ private long startTime; private long endTime; public void setStartTime(long startTime){ this.startTime=startTime; } public long getStartTime(){ return this.startTime; } public void setEndTime(long endTime){ this.endTime=endTime; } public long getEndTime(){ return this.endTime(); } }public class SessionListener implements HttpSessionListener { private static Hashtable time=new Hashtable(); public synchronized void sessionCreated(HttpSessionEvent se) { //这里创建一个你自己的BEAN对象 TimeBean timeBean=new TimeBean(); timeBean.setStartTime(System.currentTimeMillis()); se.getSession().setAttribute("time",timeBean); } public synchronized void sessionDestroyed(HttpSessionEvent se) { TimeBean timeBean=new TimeBean(); timeBean=(TimeBean)se.getSession().getAttribute("time"); timeBean.setEndTime(System.currentTimeMillis()); //存到Hashtable里 time.put(se.getSession().getId(),timeBean); } }
session创建的时候根据sessionid记录一个时间,当session销毁的时候根据sessionid再记录一个时间,同一个sessionid的两个时间差就是在线时间
//session创建记录sessionid和时间到Hashtable
}
public synchronized void sessionDestroyed(HttpSessionEvent se) {
//session销毁从Hashtable中根据sessionid取出一个时间和现在时间之差便是此游客在线时间
}
}
}注册sessionlistener到web.xml
<listener>
<listener-class>com.link2friend.friend.common.common.SessionListener</listener-class>
</listener>
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionBindingListener;
import java.io.PrintWriter;
import java.io.FileOutputStream;public final class MySessionListener
implements HttpSessionActivationListener ,HttpSessionBindingListener ,
HttpSessionAttributeListener, HttpSessionListener,ServletContextListener {
ServletContext context;
int users=0;
//HttpSessionActivationListener
public void sessionDidActivate(HttpSessionEvent se)
{
logout("sessionDidActivate("+se.getSession().getId()+")");
}
public void sessionWillPassivate(HttpSessionEvent se)
{
logout("sessionWillPassivate("+se.getSession().getId()+")");
}//HttpSessionActivationListener
//HttpSessionBindingListener
public void valueBound(HttpSessionBindingEvent event)
{
logout("valueBound("+event.getSession().getId()+event.getValue()+")");
}
public void valueUnbound(HttpSessionBindingEvent event)
{
logout("valueUnbound("+event.getSession().getId()+event.getValue()+")");
}
//HttpSessionAttributeListener
public void attributeAdded(HttpSessionBindingEvent event) {logout("attributeAdded('" + event.getSession().getId() + "', '" +
event.getName() + "', '" + event.getValue() + "')"); } public void attributeRemoved(HttpSessionBindingEvent event) {logout("attributeRemoved('" + event.getSession().getId() + "', '" +
event.getName() + "', '" + event.getValue() + "')"); }
public void attributeReplaced(HttpSessionBindingEvent se)
{
logout("attributeReplaced('"+se.getSession().getId()+",'"+se.getName()+"','"+se.getValue()+"')");
}//HttpSessionAttributeListener
//HttpSessionListener
public void sessionCreated(HttpSessionEvent event) {users++;
logout("sessionCreated('" + event.getSession().getId() + "'),目前有"+users+"个用户");
context.setAttribute("users",new Integer(users));
}
public void sessionDestroyed(HttpSessionEvent event) {users--;
logout("sessionDestroyed('" + event.getSession().getId() + "'),目前有"+users+"个用户");
context.setAttribute("users",new Integer(users)); }//HttpSessionListener
//ServletContextListener
public void contextDestroyed(ServletContextEvent sce) {logout("contextDestroyed()-->ServletContext被销毁");
this.context = null; } public void contextInitialized(ServletContextEvent sce) {this.context = sce.getServletContext();
logout("contextInitialized()-->ServletContext初始化了"); }//ServletContextListener
private void logout(String message) {
PrintWriter out=null;
try
{
out=new PrintWriter(new FileOutputStream("c:\\session.txt",true));
out.println(new java.util.Date().toLocaleString()+"::Form MySessionListener: " + message);
out.close();
}
catch(Exception e)
{
out.close();
e.printStackTrace();
}
}
}
import javax.servlet.http.*;public final class CounterListener implements HttpSessionListener {
private int count = 10;
private ServletContext context = null; public synchronized void sessionCreated(HttpSessionEvent se) {
count++;
log("sessionCreated('" + se.getSession().getId() + "'LuoTing's Log)"+" count="+count);
se.getSession().setAttribute("count",new Integer(count));
} public synchronized void sessionDestroyed(HttpSessionEvent se) {
count--;
se.getSession().setAttribute("count",new Integer(count));
} public int getCount() {
return this.count;
} public void addCount(){
count++;
} private void log(String message) { if (context != null)
context.log("SessionListener: " + message);
else
System.out.println("SessionListener: " + message); }
}
se.getSession().setAttribute("time",System.currentTimeMillis()+"");
}
public synchronized void sessionDestroyed(HttpSessionEvent se) {
long startTime=Long.parseLong(se.getSession().getAttribute("time"));
long endTime=System.currentTimeMillis();
long time=(endTime-startTime)/1000;
System.out.println("此用户在线时间为:"+time+"秒");
}
}
<listener>
<listener-class>SessionListener</listener-class>
</listener>这里注册进去就可以了,自动调用的,是监听器,只要有session创建和销毁就会分别调用sessionCreated和sessionDestroyed方法
上面统计的是每个人单个的在线时间,要在JSP页面上访问到,就把他存到application里就可以得到了
public synchronized void sessionDestroyed(HttpSessionEvent se) {
long startTime=Long.parseLong(se.getSession().getAttribute("time"));
long endTime=System.currentTimeMillis();
long time=(endTime-startTime)/1000;
System.out.println("此用户在线时间为:"+time+"秒");
se.getSession().getServletContext().setAttribute("time",time+"");
}在JSP页面上可以通过application.getAttribute("time");得到刚刚离开的用户在线时间
private long startTime;
private long endTime;
public void setStartTime(long startTime){
this.startTime=startTime;
}
public long getStartTime(){
return this.startTime;
}
public void setEndTime(long endTime){
this.endTime=endTime;
}
public long getEndTime(){
return this.endTime();
}
}public class SessionListener implements HttpSessionListener {
private static Hashtable time=new Hashtable(); public synchronized void sessionCreated(HttpSessionEvent se) {
//这里创建一个你自己的BEAN对象
TimeBean timeBean=new TimeBean();
timeBean.setStartTime(System.currentTimeMillis());
se.getSession().setAttribute("time",timeBean);
}
public synchronized void sessionDestroyed(HttpSessionEvent se) {
TimeBean timeBean=new TimeBean();
timeBean=(TimeBean)se.getSession().getAttribute("time");
timeBean.setEndTime(System.currentTimeMillis());
//存到Hashtable里
time.put(se.getSession().getId(),timeBean);
}
}