最近又有网友问到,如何用Session实现在线统计的功能,其实只要对Servlet规范详细了解一下,明白其基本原理,编写一个类似的功能并不是一件很复杂的事情。我以前的一篇文章,最初也是发表在JavaResearch上的(
http://www.javaresearch.org/article/showarticle.jsp?column=106&thread=2164),不过可能源码没有全部给出,只给出了最重点的源码,所以还是有很多网友给我发信索取全部源码。因为那个项目是以前做的,所以我手头上也没有原来的代码了,所以可能有的网友的要求就满足不了了,在这里一并致歉。
????第一篇文章是基于Servlet2.2规范编写的,那时候没有HttpSessionListener,鉴于目前绝大多数的应用服务器都支持2.3的规范,所以为了回应网友的提问,特地又基于2.3以上的规范编写了一个简单的测试例子。
这个例子的最主要功能就是提供在线用户列表显示(既然用户列表都可以显示了,那人数统计自然也不在话下了)。
????在给出代码之前,先简单说一下监听器的常识。
????HttpSessionListener:网友rdfei?在他的文章(
http://www.javaresearch.org/article/showarticle.jsp?column=106&thread=18541)
中也已经进行了比较详细的描述,这是2.3以上规范所提供的一个新功能,也就是可以定义监听器监听HttpSession对象的创建和销毁。每当有新的用
户访问网站,应用服务器会创建一个HttpSession对象,每当Session超时,应用服务器则会销毁这个对象。
HttpSessionBindingListener:每当往Session中存入一个对象(setAttribute)或从Session中删除一个对象的时候,如果这个对象实现了此监听器接口,应用服务器将会自动调用接口相应的方法。
????需要注意的一点就是,在sessionDestroyed方法和valueUnbound方法中,你可以得到HttpSession对象的实例,但是其getAttribute方法不再可用,也就是在这两个方法中,你不能再次得到存入session中的对象。
基于以上这些粗浅的认识,再简单介绍一下我提供的测试例子的情况:
测试例子总共包含如下文件:
OnlineUserListener.java:它实现了HttpSessionListener接口
OnlineUsers.java:它包含了所有正在访问网站的用户信息,为了方便起见,它也实现了HttpSessionBindingListener接口(实际中你最好把他们分开吧)
User.java:这是用户的信息
test.jsp:为了方便,我把登录,退出,显示在线用户列表等功能都做在同一个jsp文件里了。
下面是具体的代码:
-
- /**
- ?*?OnlineUserListener.java
- ?*?Created?on?2004-11-19
- ?*/
- package?com.ccctc.view.web;
-
- import?javax.servlet.http.HttpSessionEvent;
- import?javax.servlet.http.HttpSessionListener;
-
- /**
- ?*?@author?litf
- ?*?
- ?*/
- public?class?OnlineUserListener
- ????implements?HttpSessionListener?{
-
- ????/**
- ?????*?浏览器第一次访问的时候,调用本方法
- ?????*?@see?javax.servlet.http.HttpSessionListener#sessionCreated(javax.servlet.http.HttpSessionEvent)
- ?????*/
- ????public?void?sessionCreated(HttpSessionEvent?event)?{
- ????????User?u?=?new?User();
- ????????u.setName("guest");
- ????????u.setId(event.getSession().getId());
- ????????event.getSession().setAttribute("currentUser",u);
- ????????OnlineUsers.getInstance().addUser(u);
- ????}
-
- ????/**
- ?????*?Session超时的时候,调用本方法
- ?????*?@see?javax.servlet.http.HttpSessionListener#sessionDestroyed(javax.servlet.http.HttpSessionEvent)
- ?????*/
- ????public?void?sessionDestroyed(HttpSessionEvent?event)?{
- ????????OnlineUsers.getInstance().removeUser(event.getSession().getId());
- ????}
-
- }
-
-
- /**
- ?*?OnlineUsers.java
- ?*?Created?on?2004-11-19
- ?*/
- package?com.ccctc.view.web;
-
- import?java.util.Collection;
- import?java.util.HashMap;
- import?java.util.Map;
-
- import?javax.servlet.http.HttpSession;
- import?javax.servlet.http.HttpSessionBindingEvent;
- import?javax.servlet.http.HttpSessionBindingListener;
-
- /**
- ?*?@author?litf
- ?*?在线用户统计
- ?*/
- public?class?OnlineUsers?implements?HttpSessionBindingListener{
-
- ????private?Map?users?=?new?HashMap();
- ????
- ????private?static?OnlineUsers?onlineUsers?=?new?OnlineUsers();
- ????
- ????public?static?OnlineUsers?getInstance(){
- ????????return?onlineUsers;
- ????}
- ????
- ????/**
- ?????*?@return
- ?????*/
- ????public?Collection?getUsers()?{
- ????????return?users.values();
- ????}
- ????
- ????public?void?addUser(User?user)?{
- ????????users.put(user.getId(),user);
- ????}
- ????
- ????public?void?removeUser(String?userId){
- ????????users.remove(userId);
- ????}
- ????
- ????/**
- ?????*?对象实例(即OnlineUserListener的实例)作为一个属性被设置到session的
- ?????*?时候,会调用本方法,这种情况一般发生在点击登录按钮以后的处理过程中
- ?????*?设置
- ?????*?@see?javax.servlet.http.HttpSessionBindingListener#valueBound(javax.servlet.http.HttpSessionBindingEvent)
- ?????*/
- ????public?void?valueBound(HttpSessionBindingEvent?event)?{
- ????????//现在暂时不需要额外处理,你可以在这里记录日志等
- ????}
-
- ????/**
- ?????*?当Session超时,或本实例被从session中移除的时候被调用,这种情况一般
- ?????*?发生在注销方法的处理过程中
- ?????*?@see?javax.servlet.http.HttpSessionBindingListener#valueUnbound(javax.servlet.http.HttpSessionBindingEvent)
- ?????*/
- ????public?void?valueUnbound(HttpSessionBindingEvent?event)?{
- ????????try?{
- ????????????HttpSession?session?=?event.getSession();
- ????????????User?u?=?(User)session.getAttribute("currentUser");
- ????????????u.setName("guest");
- ????????}?catch?(RuntimeException?e)?{
- ????????????//e.printStackTrace();
- ????????}
- ????}
-
- ????
- }
-
-
- /**
- ?*?User.java
- ?*?Created?on?2004-11-19
- ?*/
- package?com.ccctc.view.web;
-
- /**
- ?*?@author?litf
- ?*?
- ?*/
- public?class?User{
- ????private?String?address;
- ????private?String?id;
- ????private?String?name;
-
- ????/**
- ?????*?@see?java.lang.Object#equals(java.lang.Object)
- ?????*/
- ????public?boolean?equals(Object?obj)?{
- ????????if?(obj?==?null?||?!(obj?instanceof?User))?{
- ????????????return?false;
- ????????}
- ????????if(this.id?!=null?&&?this.id.equals(((User)obj).getId())){
- ????????????return?true;
- ????????}
- ????????return?false;
- ????}
-
- ????/**
- ?????*?@return
- ?????*/
- ????public?String?getAddress()?{
- ????????return?address;
- ????}
-
- ????/**
- ?????*?@return
- ?????*/
- ????public?String?getId()?{
- ????????return?id;
- ????}
-
- ????/**
- ?????*?@return
- ?????*/
- ????public?String?getName()?{
- ????????return?name;
- ????}
-
- ????/**
- ?????*?@param?string
- ?????*/
- ????public?void?setAddress(String?string)?{
- ????????address?=?string;
- ????}
-
- ????/**
- ?????*?@param?string
- ?????*/
- ????public?void?setId(String?string)?{
- ????????id?=?string;
- ????}
-
- ????/**
- ?????*?@param?string
- ?????*/
- ????public?void?setName(String?string)?{
- ????????name?=?string;
- ????}
- ????
- ????/**
- ?????*?@see?java.lang.Object#toString()
- ?????*/
- ????public?String?toString()?{
- ????????return?"name:"+name?+?",id:"?+?id?+?",address:"+address;
- ????}
-
- }
-
test.jsp
-
- <%@?page?import="com.ccctc.view.web.*"?%>
-
- <%
- User?u?=?(User)session.getAttribute("currentUser");
- String?name?=?request.getParameter("user");
-
- if(u?!=?null){
- ????String?remote?=?request.getRemoteAddr();
- ????u.setAddress(remote);
- ????
- }
-
- //User?Login
- if(name?!=?null){
- ????if(u?!=?null){
- ????????session.setAttribute("_listener",OnlineUsers.getInstance());
- ????????u.setName(name);
- ????????
- ????}
- }
-
- //User?Logout
- String?logout?=?request.getParameter("logout");
- if(logout?!=?null){
- ????session.removeAttribute("_listener");
- }
- %>
-
- current?users:
-
- <%?
- java.util.Collection?l?=?OnlineUsers.getInstance().getUsers();
- for(java.util.Iterator?it?=?l.iterator();?it.hasNext();)
- {
- ????User?tu?=?(User)it.next();
- ????if(u.equals(tu))
- ????{
- %>
-
- <font?color=red><%=tu.getName()%>:<%=tu.getAddress()%>???
-
- <%
- ????}else{
- %>
-
- <%=tu.getName()%>:<%=tu.getAddress()%>??
-
- <%?
- ????}
- }
- %>
- <form?action=<font color="#ff33ff">"/cctc/login.jsp">
- User?:?<input?name=<font color="#ff33ff">"user"?type="text">?
- <input?type=<font color="#ff33ff">"submit"?name="Submit">
-
- <a?href=<font color="#ff33ff">"/cctc/login.jsp?logout=true">Logout?
- <a?href=<font color="#ff33ff">"/cctc/login.jsp">refresh
-
web.xml文件中增加:
-
- ????<listener>
- ????????<listener-<b>class>com.ccctc.view.web.OnlineUserListener</listener-<b>class>
- ????</listener>
JSP环境基于Session的在线用户统计深入分析
JSP作为后起之秀能够在服务器编程环境中占据一定地位,是和它良好支持一系列业界标准
密切相关的。Session就是它提供的基础设施之一。作为一个程序员,你可以不介意具体在
客户端是如何实现,就方便的实现简单的基于session的用户管理。
现在对于处理在线用户,有几种不同的处理方法。
一种是叶面刷新由用户控制,服务器端控制一个超时时间比如30分钟,到了时间之后用户
没有动作就被踢出。这种方法的优点是,如果用户忘了退出,可以防止别人恶意操作。
缺点是,如果你在做一件很耗时间的事情,超过了这个时间限制,submit的时候可能要
再次面临登陆。如果原来的叶面又是强制失效的话,就有可能丢失你做的工作。在实现
的角度来看,这是最简单的,Server端默认实现的就是这样的模式。
另一种方式是,站点采用框架结构,有一个Frame或者隐藏的iframe在不断刷新,这样你
永远不会被踢出,但是服务器端为了判断你是否在线,需要定一个发呆时间,如果超过
这个发呆时间你除了这个自动刷新的叶面外没有刷新其他叶面的话,就认为你已经不在
线了。采取这种方式的典型是xici.net。 他的优点是可以可以利用不断的刷新实现一些
类似server-push的功能,比如网友之间发送消息。
不管哪一种模式,为了实现浏览当前所有的在线用户,还需要做一些额外的工作。
servlet API中没有得到Session列表的API。
可以利用的是Listener. Servlet 2.2和2.3规范在这里略微有一些不一样。
2.2中HttpSessionBindingListener可以实现当一个HTTPSession中的Attribute变化的
时候通知你的类。而2.3中还引入了HttpSessionAttributeListener.鉴于我使用的环境
是Visual age for java 4和JRun server 3.1,他们还不直接支持Servlet 2.3的编程,
这里我用的是HttpSessionBindingListener.
需要做的事情包括做一个新的类来实现HttpSessionBindingListener接口。这个接口有
两个方法:
public void valueBound(HttpSessionBindingEvent event),和
public void valueUnbound(HttpSessionBindingEvent event)。
当你执行Session.addAttribute(String,Object)的时候,如果你已经把一个实现了
HttpSessionBindingListener接口的类加入为Attribute,Session会通知你的类,调用
你的valueBound方法。相反,Session.removeAttribute方法对应的是valueUndound方法。
public class HttpSessionBinding implements javax.servlet.http.HttpSessionBindingListener
{
ServletContext application = null;
public HttpSessionBinding(ServletContext application)
{
super();
if (application ==null)
throw new IllegalArgumentException("Null application is not accept.");
this.application = application;
}
public void valueBound(javax.servlet.http.HttpSessionBindingEvent e)
{
Vector activeSessions = (Vector) application.getAttribute("activeSessions");
if (activeSessions == null)
{
activeSessions = new Vector();
}
JDBCUser sessionUser = (JDBCUser)e.getSession().getAttribute("user");
if (sessionUser != null)
{
activeSessions.add(e.getSession());
}
application.setAttribute("activeSessions",activeSessions);
}
public void valueUnbound(javax.servlet.http.HttpSessionBindingEvent e)
{
JDBCUser sessionUser = (JDBCUser)e.getSession().getAttribute("user");
if (sessionUser == null)
{
Vector activeSessions = (Vector) application.getAttribute("activeSessions");
if (activeSessions != null)
{
activeSessions.remove(e.getSession().getId());
application.setAttribute("activeSessions",activeSessions);
}
}
}
}
假设其中的JDBCUser类是一个任意User类。
在执行用户登录时,把User类和HttpSessionBinding类都加入到Session中去。
这样,每次用户登录后,在application中的attribute "activeSessions"这个vector中
都会增加一条记录。
每当session超时,valueUnbound被触发,在这个vector中删去将要被超时的session.
public void login()
throws ACLException,SQLException,IOException
{
/* get JDBC User Class */
if (user != null)
{
logout();
}
{
// if session time out, or user didn't login, save the target url temporary.
JDBCUserFactory uf = new JDBCUserFactory();
if ( (this.request.getParameter("userID")==null)
|| (this.request.getParameter("password")==null) )
{
throw new ACLException("Please input a valid userName and password.");
}
JDBCUser user =
(JDBCUser) uf.UserLogin(
this.request.getParameter("userID"),
this.request.getParameter("password") );
user.touchLoginTime();
this.session.setAttribute("user",user);
this.session.setAttribute("BindingNotify",new HttpSessionBinding(application));
}
}
Login的时候,把User和这个BindingNotofy目的的类都加入到session中去。
logout的时候,就要主动在activeSessions这个vector中删去这个session.
public void logout()
throws SQLException,ACLException
{
if (this.user == null
&& this.session.getAttribute("user")==null)
{
return;
}
Vector activeSessions = (Vector) this.application.getAttribute("activeSessions");
if (activeSessions != null)
{
activeSessions.remove(this.session);
application.setAttribute("activeSessions",activeSessions);
}
java.util.Enumeration e = this.session.getAttributeNames();
while (e.hasMoreElements())
{
String s = (String)e.nextElement();
this.session.removeAttribute(s);
}
this.user.touchLogoutTime();
this.user = null;
}
这两个函数位于一个HttpSessionManager类中.这个类引用了jsp里面的application全局
对象。
这个类的其他代码和本文无关且相当长,我就不贴出来了。
下面来看看jsp里面怎么用。
假设一个登录用的表单被提交到doLogin.jsp, 表单中包含UserName和password域。
节选部分片段:
<%
HttpSessionManager hsm = new HttpSessionManager(application,request,response);
try
{
hsm.login();
}
catch ( UserNotFoundException e)
{
response.sendRedirect("InsufficientPrivilege.jsp?detail=User%20does%20not%20exist.");
return;
}
catch (InvalidPasswordException e2)
{
response.sendRedirect("InsufficientPrivilege.jsp?detail=Invalid%20Password");
return;
}
catch ( Exception e3)
{
%>Error:<%=e3.toString() %>
Press Here to relogin.
<%return;
}
response.sendRedirect("index.jsp");
%>
再来看看现在我们怎么得到一个当前在线的用户列表。
<body bgcolor="#FFFFFF">
<%
Vector activeSessions = (Vector) application.getAttribute("activeSessions");
if (activeSessions == null)
{
activeSessions = new Vector();
application.setAttribute("activeSessions",activeSessions);
}
Iterator it = activeSessions.iterator();
while (it.hasNext())
{
HttpSession sess = (HttpSession)it.next();
JDBCUser sessionUser = (JDBCUser)sess.getAttribute("user");
String userId = (sessionUser!=null)?sessionUser.getUserID():"None";
%>
<%
}
%>
SessionId | User | Login Time | Last access Time |
<%= sess.getId() %> | <%= userId %> | <%= BeaconDate.getInstance( new java.util.Date(sess.getCreationTime())).getDateTimeString()%> | <%= BeaconDate.getInstance( new java.util.Date(sess.getLastAccessedTime())).getDateTimeString()%> |
</body>
以上的代码从application中取出activeSessions,并且显示出具体的时间。其中
BeaconDate类假设为格式化时间的类。
这样,我们得到了一个察看在线用户的列表的框架。至于在线用户列表分页等功能,
与本文无关,不予讨论。
这是一个非刷新模型的例子,依赖于session的超时机制。我的同事sonymusic指出很
多时候由于各个厂商思想的不同,这有可能是不可信赖的。考虑到这种需求,需要在
每个叶面刷新的时候都判断当前用户距离上次使用的时间是否超过某一个预定时间值。
这实质上就是自己实现session超时。
如果需要实现刷新模型,就必须使用这种每个叶面进行刷新判断的方法。
分享到:
相关推荐
使用简单的范例描述session统计在线人数的原理,内有源码及注释。
JavaWeb 用session统计在线人数
JAVA的SESSION精准统计 消除了SESSION可以为负数的BUG... 更加精准
简单测试,session统计上线人数demo
该例程利用session全面解决了用户在线数量统计及控制用户重复登录。
基于Session的在线用户统计实现 基于Session的在线用户统计实现
servlet 服务器监视所有的session创建,同时可以实现网站流量以及当前在线人数的统计
JSP中基于Session的在线用户统计分析
一个用客户端js脚本函数进行网页Session超期统计个数显示函数库,采用纯js实现,可以动态监测Web网页Session超期的个数。
本文实例讲述了PHP基于cookie与session统计网站访问量并输出显示的方法。分享给大家供大家参考,具体如下: <?php $f_open = fopen("count.txt","r+"); //打开指定的文件 $count = fgets($f_open); //...
该例程利用session全面解决了用户在线数量统计及控制用户重复登录。
主要介绍了php使用Session和文件统计在线人数,本文直接给出实例代码,需要的朋友可以参考下
用户登录时,调用LoginServlet,将用户名写入application的在线用户列表中,用户退出时,调用LogoutServlet,调用session.invalidate(),交给HttpSessionListener的sessionDestroyed()方法,将用户从在线列表中删除。...
你可以通过该session管理容器进行登录判断 在线用户统计 session清理 用户在线信息统一管理 多种操作 本组件源自一个商业项目开发中的灵感 本组件性能高效,通过一个Map来管理所有用户的session引用
juniper网关会话分析,再也不用在命令行下面一条一条的看,SESSION一下就能全部统计出来,并根据需要做出分析,帮助网管同志们,查看SESSION的占用情况
数据库:Druid数据库连接池,监控数据库访问性能,统计SQL的执行性能。 持久层:mybatis持久化,使用MyBatis-Plus优化,减少sql开发量;aop切换数据库实现读写分离。Transtraction注解事务。 MVC: 基于spring mvc...
利用session统计访问页面的人数,并且把得到的数字写到文件里
主要介绍了Spring boot通过HttpSessionListener监听器统计在线人数的实现代码,非常不错,具有参考借鉴价值,需要的朋友可以参考下
session理解临床试验的项目管理医疗器械临床试验管理与统计分析培训班讲义PPT学习教案.pptx