关于JSP中基于Session的在线用户统计分析

2008-06-20 09:08:01.0     推荐:0    收藏:0    评论:4     来源:赛迪网

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超时。如果需要实现刷新模型,就必须使用这种每个叶面进行刷新判断的方法。

[上一页]   [第1页]   [第2页]  
您可以针对本文进行:[评论]  [收藏]  [推荐]  
  • [4楼] 2008-08-04 17:19:13.0  网友
  • [回复]
  • [url=http://www.at-fl.com]俄语培训[/url]
    [url=http://www.at-fl.com]意大利语培训[/url]
    [url=http://www.at-fl.com]意大利语学习[/url]
    [url=http://www.at-fl.com]德语培训[/url]
    [url=http://www.at-fl.com]西班牙语培训[/url] 
  • [3楼] 2008-06-23 07:36:09.0  网友
  • [回复]
  • <a href="http://www.shutongguandao.net.cn/">管道疏通</a>
    <a href="http://www.shutongguandao.net.cn/guandaoshutong3.htm">疏通管道</a>
    <a href="http://www.shutongguandao.net.cn/guandaoshutong3.htm">北京疏通下水道</a>
    <a href="http://www.shutongguandao.net.cn/guandaoshutong3.htm">疏通下水道</a>
    <a href="http://www.shutongguandao.net.cn/guandaoshutong3.htm">北京疏通管道</a>
    <a href="http://www.shutongguandao.net.cn/guandaoshutong.htm">专业疏通管道</a>
    <a href="http://www.shutongguandao.net.cn/guandaoshutong.htm">专业疏通</a>
  • [2楼] 2008-06-23 07:32:46.0  网友
  • [回复]
  •  <a href="http://www.shutongguandao.net.cn/huafenchi.html" target="_blank">高压清洗</a> <a href="http://www.shutongguandao.net.cn/huafenchi.html" target="_blank">化粪池清理</a> <a href="http://www.shutongguandao.net.cn/huafenchi.html" target="_blank">抽粪</a> <a href="http://www.shutongguandao.net.cn/huafenchi.html" target="_blank">北京抽粪</a> <a href="http://www.shutongguandao.net.cn/huafenchi.html" target="_blank">清理化粪池</a>
    <a href="http://www.shutongguandao.net.cn/shutongmatong.htm">北京疏通马桶</a>
  • [1楼] 2008-06-23 07:32:34.0  网友
  • [回复]
  • <A href=http://www.at-fl.com>俄语培训</A> 
    <A href=http://www.at-fl.com>意大利语培训</A> 
    <A href=http://www.at-fl.com>意大利语学习</A> 
    <A href=http://www.at-fl.com>德语培训</A> 
    <A href=http://www.at-fl.com>西班牙语培训</A> 
    <A href=http://www.at-fl.com>西班牙语学习</A> 
    <A href=http://www.at-fl.com>法语培训</A> 
  • 共有4条评论  点击查看更多评论
  • 网友评论仅供网友表达个人看法,并不表明e800同意其观点或证实其描述
我想发表评论:
用户名密码
  • 匿名发表
    验证码: