Servlet监听器统计在线人数

监听器的作用是监听Web容器的有效事件,它由Servlet容器管理,利用Listener接口监听某个执行程序,并根据该程序的需求做出适应的响应。

例1 应用Servlet监听器统计在线人数。

(1)创建UserInfoList.java类文件,主要是用来存储在线用户和对在线用户进行具体操作。该文件的完整代码如下:

package com.cn.gao;



import java.util.Vector;



public class UserInfoList {

    private static UserInfoList user = new UserInfoList();

    private Vector vector = null;

    /*

     * 利用private调用构造函数,防止被外界产生新的instance对象

     */

    public UserInfoList(){

        this.vector = new Vector();

    }

    

    //外界使用的instance对象

    public static UserInfoList getInstance(){

        return user;

    }

    

    //增加用户

    public boolean addUserInfo(String user){

        if(user!=null){

            this.vector.add(user);

            return true;

        }else{

            return false;

        }

    }

    

    //获取用户列表

    public Vector getList(){

        return vector;

    }

    

    //移除用户

    public void removeUserInfo(String user){

        if(user!=null){

            vector.removeElement(user);

        }

    }

}

(2)创建UserInfoTrace.java类文件,主要实现valueBound(HttpSessionBindingEvent arg0)和valueUnbound(HttpSessionBindingEvent arg0)两个方法。当有对象加入session时,valueBound()方法会自动被执行;当有对象从session中移除时,valueUnbound()方法会被自动执行,在valueBound()和valueUnbound()方法中都加入了输出信息的功能,可使用户在控制台中更清楚地了解执行过程。该文件的完整代码如下:

 

package com.cn.gao;



import javax.servlet.http.HttpSessionBindingEvent;

import javax.servlet.http.HttpSessionBindingListener;



public class UserInfoTrace implements HttpSessionBindingListener {

    private String user;

    private UserInfoList container = UserInfoList.getInstance();

    

    public UserInfoTrace(){

        user = "";

    }

    

    //设置在线监听人员

    public void setUser(String user){

        this.user = user;

    }

    

    //获取在线监听

    public String getUser(){

        return this.user;

    }

    

    public void valueBound(HttpSessionBindingEvent arg0) {

        System.out.println("上线"+this.user);

    }



    public void valueUnbound(HttpSessionBindingEvent arg0) {

        System.out.println("下线"+this.user);

        if(user!=""){

            container.removeUserInfo(user);

        }



    }



}

 

(3)创建index2.jsp页面文件,用于用户登录,该页面的关键代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

    

    <title>My JSP 'index2.jsp' starting page</title>

    

    <meta http-equiv="pragma" content="no-cache">

    <meta http-equiv="cache-control" content="no-cache">

    <meta http-equiv="expires" content="0">    

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

    <meta http-equiv="description" content="This is my page">

    <!--

    <link rel="stylesheet" type="text/css" href="styles.css">

    -->



  </head>

  

  <body>

    <form action="showUser.jsp" method="post" >

        <table align="center">

            <tr>

                <td align="center" class="2">在线系统</td>

            </tr>

            <tr>

                <td align="center">

                    <input type="text" name="user"/>

                </td>

            </tr>

            <tr>

                <td align="center">

                    <input type="submit" value="登陆"/>

                </td>

            </tr>

        </table>

    </form>

  </body>

</html>

 

 

(3)创建showUser.jsp页面文件,在页面中设置session的setMaxInactiveInterval()为10秒,这样可以缩短session的生命周期。该页面的关键代码如下:

 

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>

<%@ page import="com.cn.gao.*" %>

<%

UserInfoList list = UserInfoList.getInstance();

UserInfoTrace ut = new UserInfoTrace();

String name=request.getParameter("user");

ut.setUser(name);

session.setAttribute("list",ut);

list.addUserInfo(ut.getUser());

session.setMaxInactiveInterval(10);

%>



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>   

    <title>My JSP 'showUser.jsp' starting page</title>

    

    <meta http-equiv="pragma" content="no-cache">

    <meta http-equiv="cache-control" content="no-cache">

    <meta http-equiv="expires" content="0">    

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

    <meta http-equiv="description" content="This is my page">

    <!--

    <link rel="stylesheet" type="text/css" href="styles.css">

    -->



  </head>

  

  <body>

  <form method="post">

      <table align="center">

          <tr>

              <td align="center" class="2">

                  <h2>在线用户名单</h2>

              </td>

          </tr>

          <tr>

              <td align="center">

                  <textarea rows="8" cols="20"><%

                      Vector vector=list.getList();

                      if(vector!=null&&vector.size()>0){

                          for(int i=0;i<vector.size();i++){

                              out.println(vector.elementAt(i));

                          }

                      }

                   %>

                  </textarea>

              </td>

          </tr>

      </table>

  </form>

  </body>

</html>

当用户单击“登录”按钮,就会进入到统计在线人数的界面。

你可能感兴趣的:(servlet)