假設(shè)其中的JDBCUser類是一個任意User類。在執(zhí)行用戶登錄時,把User類和HttpSessionBinding類都加入到Session中去。
這樣,每次用戶登錄后,在application中的attribute "activeSessions"這個vector中都會增加一條記錄。每當(dāng)session超時,valueUnbound被觸發(fā),在這個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;
}
這兩個函數(shù)位于一個HttpSessionManager類中.這個類引用了jsp里面的application全局對象。這個類的其他代碼和本文無關(guān)且相當(dāng)長,我就不貼出來了。
相關(guān)推薦:2010年全國計算機(jī)等級考試考試報考指南北京 | 天津 | 上海 | 江蘇 | 山東 |
安徽 | 浙江 | 江西 | 福建 | 深圳 |
廣東 | 河北 | 湖南 | 廣西 | 河南 |
海南 | 湖北 | 四川 | 重慶 | 云南 |
貴州 | 西藏 | 新疆 | 陜西 | 山西 |
寧夏 | 甘肅 | 青海 | 遼寧 | 吉林 |
黑龍江 | 內(nèi)蒙古 |