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() %><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 >sessionid
</td>
<td >user
</td>
<td >login time
</td>
<td >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超時。
如果需要實現刷新模型,就必須使用這種每個葉面進行刷新判斷的方法。
新聞熱點
疑難解答