用HttpSessionListener與HttpSessionBindingListener實現在線人數統計

          想了一下在線人數統計方面的實現,上網找了下這方面的知識,最初我的想法是,管理session,如果session銷毀了就減少,如果登陸用戶了就新增一個,但是如果是用戶非法退出,如:未注銷,關閉瀏覽器等,這個用戶的session是管理不到的,最后決定用HttpSessionListener接口或HttpSessionBindingListener接口來實現,通過監聽session的新建和銷毀來控制,詳細如下。

          先添加登陸的頁面index.jsp

           1<%@ page contentType="text/html;charset=utf-8"%>
           2<html>
           3<head>
           4<title>test</title>              
           5</head>
           6<body>
           7<form action="login.jsp" method="post">
           8    用戶名:<input type="text" name="username" />
           9    <br />
          10    <input type="submit" value="登錄" />
          11</form>
          12</body>
          13</html>
          14

          點擊登陸后跳轉的login.jsp(為了方便,用jsp做servlet,同學們用的時候記得改過來)

           1<%@ page contentType="text/html;charset=utf-8"%>
           2<%@ page import="java.util.*"%>
           3<%
           4    request.setCharacterEncoding("UTF-8");
           5    // 取得登錄的用戶名
           6    String username = request.getParameter("username");
           7    // 把用戶名保存進session
           8    session.setAttribute("username", username);
           9    // 把用戶名放入在線列表
          10    List onlineUserList = (List) application.getAttribute("onlineUserList");
          11    // 第一次使用前,需要初始化
          12    if (onlineUserList == null) {
          13        onlineUserList = new ArrayList();
          14        application.setAttribute("onlineUserList", onlineUserList);
          15    }
          16    onlineUserList.add(username);
          17    // 成功
          18    response.sendRedirect("result.jsp");
          19
          %>
          20

          登陸成功跳轉到顯示頁面result.jsp

          1<%@ page contentType="text/html;charset=utf-8"%>
          2<%@ page isELIgnored="false"%>
          3<%@page import="java.util.List"%>
          4
          <h3>您好:${username} [<a href="logout.jsp">注銷</a>]</h3>
          當前在線用戶:
          <table>
          <%
          List onlineUserList = (List) application.getAttribute("onlineUserList");
          for (int i = 0; i < onlineUserList.size(); i++) {
          String onlineUsername = (String) onlineUserList.get(i);
          %>
          <tr>
          <td><%=onlineUsername%></td>
          </tr>
          <%
          }
          %>
          </table>

          點擊注銷頁面logout.jsp頁面

           1<%@ page contentType="text/html;charset=utf-8"%>
           2<%@ page import="java.util.*"%>
           3<%
           4    // 取得登錄的用戶名
           5    String username = (String) session.getAttribute("username");
           6    // 銷毀session
           7    session.invalidate();
           8    // 從在線列表中刪除用戶名
           9    List onlineUserList = (List) application.getAttribute("onlineUserList");
          10    onlineUserList.remove(username);
          11    // 成功
          12    response.sendRedirect("index.jsp");
          13
          %>
          14

          OK,登陸、查看、注銷頁面都有了,下面開始新建監聽器

          1、HttpSessionListener添加類OnlineUserListener,繼承HttpSessionListener,HttpSessionListener中有兩個方法sessionCreated(HttpSessionEvent
          event)與sessionDestroyed(HttpSessionEvent ,前者是監聽session的新建,后者是監聽session的銷毀。

           OnlineUserListener代碼如下:

           1package com.test;
           2
           3import java.util.List;
           4import javax.servlet.ServletContext;
           5import javax.servlet.http.HttpSession;
           6import javax.servlet.http.HttpSessionEvent;
           7import javax.servlet.http.HttpSessionListener;
           8/**
           9 * @author 版本
          10 */

          11public class OnlineUserListener implements HttpSessionListener {
          12
          13    public void sessionCreated(HttpSessionEvent event) {
          14        System.out.println("新建session:"+event.getSession().getId());
          15    }

          16    public void sessionDestroyed(HttpSessionEvent event) {
          17        HttpSession session = event.getSession();
          18        ServletContext application = session.getServletContext();
          19        // 取得登錄的用戶名
          20        String username = (String) session.getAttribute("username");
          21        // 從在線列表中刪除用戶名
          22        List onlineUserList = (List) application.getAttribute("onlineUserList");
          23        onlineUserList.remove(username);
          24        System.out.println(username+"已經退出!");
          25    }

          26}

          27

           web.xml配置:

          1<listener>
          2  <listener-class>com.test.OnlineUserListener</listener-class>
          3</listener>
          4

          2、HttpSessionBindingListenerHttpSessionBindingListener雖然叫做監聽器,但使用方法與HttpSessionListener完全不同。我們實際看一下它是如何使用的。

          新建類OnlineUserBindingListener,實現HttpSessionBindingListener接口,構造方法傳入username參數,HttpSessionBindingListener內有兩個方法valueBound(HttpSessionBindingEvent
          event)和valueUnbound(HttpSessionBindingEvent event),前者為數據綁定,后者為取消綁定

          所謂對session進行數據綁定,就是調用session.setAttribute()把HttpSessionBindingListener保存進session中。

           1<%@page import="com.test.OnlineUserBindingListener"%>
           2<%@ page contentType="text/html;charset=utf-8"%>
           3<%@ page import="java.util.*"%>
           4<%
           5    request.setCharacterEncoding("UTF-8");
           6    // 取得登錄的用戶名
           7    String username = request.getParameter("username");
           8       // 把用戶名放入在線列表
           9    session.setAttribute("onlineUserBindingListener"new OnlineUserBindingListener(username));
          10    // 成功
          11    response.sendRedirect("result.jsp");
          12
          %>
          13

          這就是HttpSessionBindingListener和HttpSessionListener之間的最大區別:HttpSessionListener只需要設置到web.xml中就可以監聽整個應用中的所有session。HttpSessionBindingListener必須實例化后放入某一個session中,才可以進行監聽。

          監聽范圍上比較,HttpSessionListener設置一次就可以監聽所有session,HttpSessionBindingListener通常都是一對一的。

          正是這種區別成就了HttpSessionBindingListener的優勢,我們可以讓每個listener對應一個username,這樣就不需要每次再去session中讀取username,進一步可以將所有操作在線列表的代碼都移入listener,更容易維護。

          HttpSessionBindingListener代碼如下:

           1package com.test;
           2
           3import java.util.ArrayList;
           4import java.util.List;
           5import javax.servlet.ServletContext;
           6import javax.servlet.http.HttpSession;
           7import javax.servlet.http.HttpSessionBindingEvent;
           8import javax.servlet.http.HttpSessionBindingListener;
           9
          10public class OnlineUserBindingListener implements HttpSessionBindingListener {
          11    String username;
          12    
          13    public OnlineUserBindingListener(String username){
          14        this.username=username;
          15    }

          16    public void valueBound(HttpSessionBindingEvent event) {
          17        HttpSession session = event.getSession();
          18        ServletContext application = session.getServletContext();
          19        // 把用戶名放入在線列表
          20        List onlineUserList = (List) application.getAttribute("onlineUserList");
          21        // 第一次使用前,需要初始化
          22        if (onlineUserList == null{
          23            onlineUserList = new ArrayList();
          24            application.setAttribute("onlineUserList", onlineUserList);
          25        }

          26        onlineUserList.add(this.username);
          27    }

          28
          29    public void valueUnbound(HttpSessionBindingEvent event) {
          30        HttpSession session = event.getSession();
          31        ServletContext application = session.getServletContext();
          32
          33        // 從在線列表中刪除用戶名
          34        List onlineUserList = (List) application.getAttribute("onlineUserList");
          35        onlineUserList.remove(this.username);
          36        System.out.println(this.username + "退出。");
          37
          38    }

          39
          40}

          41

           


           

          在login.jsp中做這一步:

          b.xml配置:

          web.xml配置:

          posted on 2011-01-07 19:25 子恩 閱讀(593) 評論(1)  編輯  收藏 所屬分類: javaSE

          評論

          # re: 用HttpSessionListener與HttpSessionBindingListener實現在線人數統計[未登錄] 2011-01-11 10:11 a

          關于“實現在線人數統計”功能,可以參考開源論壇JForum的實現。你分析的也不錯啊。  回復  更多評論   


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2011年1月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          導航

          統計

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          搜索

          最新評論

          主站蜘蛛池模板: 龙南县| 吉安县| 高阳县| 长子县| 射洪县| 昌图县| 谢通门县| 都昌县| 保靖县| 读书| 内丘县| 资阳市| 舟曲县| 沽源县| 灵丘县| 久治县| 兴安盟| 吴忠市| 聂荣县| 大冶市| 莲花县| 寿阳县| 赤峰市| 沾益县| 崇信县| 聊城市| 砀山县| 稷山县| 沅江市| 青阳县| 双柏县| 阿拉善左旗| 裕民县| 四平市| 惠州市| 黄龙县| 肥西县| 湖南省| 玛纳斯县| 桦川县| 台州市|