熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

關於JSP中基於Session的在線用戶統計分析[2]

2013-11-15 12:18:24  來源: JSP教程 

  假設其中的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);
  %>

[]  []  []  


From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20685.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.