假设其中的JDBCUser类是一个任意User类在执行用户登录时把User类和HttpSessionBinding类都加入到Session中去
这样每次用户登录后在application中的attribute activeSessions这个vector中都会增加一条记录每当session超时valueUnbound被触发在这个vector中删去将要被超时的session
public void login()
throws ACLExceptionSQLExceptionIOException
{
/* get JDBC User Class */
if (user != null)
{
logout();
}
{
// if session time out or user didnt login save the target url temporary
JDBCUserFactory uf = new JDBCUserFactory();
if ( (thisrequestgetParameter(userID)==null) || (thisrequestgetParameter(password)==null) )
{
throw new ACLException(Please input a valid userName and password);
}
JDBCUser user = (JDBCUser) ufUserLogin(
thisrequestgetParameter(userID)
thisrequestgetParameter(password) );
usertouchLoginTime();
thissessionsetAttribute(useruser);
thissessionsetAttribute(BindingNotifynew HttpSessionBinding(application));
}
}
Login的时候把User和这个BindingNotofy目的的类都加入到session中去logout的时候就要主动在activeSessions这个vector中删去这个session
public void logout()
throws SQLExceptionACLException
{
if (thisuser == null && thissessiongetAttribute(user)==null)
{
return;
}
Vector activeSessions = (Vector) thisapplicationgetAttribute(activeSessions);
if (activeSessions != null)
{
activeSessionsremove(thissession);
applicationsetAttribute(activeSessionsactiveSessions);
}
javautilEnumeration e = thissessiongetAttributeNames();
while (ehasMoreElements())
{
String s = (String)enextElement();
thissessionremoveAttribute(s);
}
thisusertouchLogoutTime();
thisuser = null;
}
这两个函数位于一个HttpSessionManager类中这个类引用了jsp里面的application全局对象这个类的其他代码和本文无关且相当长我就不贴出来了
下面来看看JSP里面怎么用
假设一个登录用的表单被提交到doLoginjsp 表单中包含UserName和password域节选部分片段
<%
HttpSessionManager hsm = new HttpSessionManager(applicationrequestresponse);
try
{
hsmlogin();
}
catch ( UserNotFoundException e)
{
responsesendRedirect(InsufficientPrivilegejsp?detail=User%does%not%exist);
return;
}
catch ( InvalidPasswordException e)
{
responsesendRedirect(InsufficientPrivilegejsp?detail=Invalid%Password);
return;
}
catch ( Exception e)
{
%> Error:<%=etoString() %><br>
Press <a href=loginjsp>Here</a> to relogin
<% return;
}
responsesendRedirect(indexjsp);
%>
[] [] []