服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

关于jsp中基于session的在线用户统计分析 (1)

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() %><br> 

 press <a href="login.jsp">here</a> to relogin. 

 <% return; 

} 

response.sendredirect("index.jsp"); 

%>

再来看看现在我们怎么得到一个当前在线的用户列表。

<body bgcolor="#ffffff"> 

<table cellspacing="0" cellpadding="0" width="100%"> 


<tr > 

<td style="width:24px">sessionid 

</td> 

<td style="width:80px" >user 

</td> 

<td style="width:80px" >login time 

</td> 

<td style="width:80px" >last access time 

</td> 

</tr> 

<% 

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"; 

%> 

<tr> 

<td nowrap=''><%= sess.getid() %></td> 

<td nowrap=''><%= userid %></td> 

<td nowrap=''> 

<%= beacondate.getinstance( new java.util.date(sess.getcreationtime())).
getdatetimestring()%></td> 

<td class="<%= stl %>3" nowrap=''> 

<%= beacondate.getinstance( new java.util.date(sess.getlastaccessedtime())).
getdatetimestring()%></td> 

</tr> 

<% 

} 

%> 

</table> 

</body>

以上的代码从application中取出activesessions,并且显示出具体的时间。其中beacondate类假设为格式化时间的类。

这样,我们得到了一个察看在线用户的列表的框架。至于在线用户列表分页等功能,与本文无关,不予讨论。

这是一个非刷新模型的例子,依赖于session的超时机制。我的同事sonymusic指出很多时候由于各个厂商思想的不同,这有可能是不可信赖的。考虑到这种需求,需要在每个叶面刷新的时候都判断当前用户距离上次使用的时间是否超过某一个预定时间值。这实质上就是自己实现session超时。如果需要实现刷新模型,就必须使用这种每个叶面进行刷新判断的方法。

扫描关注微信公众号