<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 15,  comments - 8,  trackbacks - 0
    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 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超時。如果需 要實現刷新模型,就必須使用這種每個葉面進行刷新判斷的方法。
    posted on 2008-09-17 18:10 lvq810 閱讀(338) 評論(0)  編輯  收藏 所屬分類: Java
    主站蜘蛛池模板: 一级毛片免费播放男男| 亚洲久热无码av中文字幕| 中文字幕不卡高清免费| 免费一看一级毛片全播放| 国产精品亚洲专区无码不卡| 日韩毛片无码永久免费看| 美女视频黄免费亚洲| 在线观看免费人成视频色| 亚洲av片不卡无码久久| 国产免费av片在线看| 亚洲天然素人无码专区| 好爽…又高潮了毛片免费看| 亚洲欧美成人一区二区三区| 国产一区视频在线免费观看| 野花视频在线官网免费1| 国产成人亚洲综合a∨| 国产真实伦在线视频免费观看| 亚洲综合激情另类专区| a级毛片免费播放| 日产亚洲一区二区三区| 大学生一级毛片免费看| 久久精品国产亚洲av天美18| 亚洲国产成人影院播放| 亚洲第一视频网站| 亚洲欧洲av综合色无码| 亚洲免费日韩无码系列| 成全视频高清免费观看电视剧 | 亚洲成A人片77777国产| 三年在线观看免费观看完整版中文| 亚洲日韩乱码中文无码蜜桃臀网站| 久久永久免费人妻精品| 亚洲人成网站日本片| 免费在线观看黄网站| 国产在线精品免费aaa片| 亚洲综合校园春色| 久久亚洲国产精品123区| 亚洲免费在线观看视频| 一级做a爰全过程免费视频毛片| 久久久久亚洲av无码尤物| 在线观看成人免费| 97在线免费视频|