Servlet监听器统计在线人数

image.png
image.png

UserInfo

package com.baozi;

import java.util.Vector;

public class UserInfo {
    @SuppressWarnings("rawtypes")
    private Vector vec=new Vector();
    private static UserInfo user=new UserInfo();
    @SuppressWarnings("rawtypes")
    public UserInfo(){
        vec=new Vector();
    }
    public static UserInfo getInstance(){
        return user;
    }
    @SuppressWarnings("unchecked")
    public boolean addUserInfo(String user){
        if(user!=null){
            vec.addElement(user);
            return true;
        }
        return false;
    }
    @SuppressWarnings("rawtypes")
    public Vector getUserInfo(){
        return vec;
    }
    public void removeUserInfo(String user){
        vec.removeElement(user);
    }
    
}

UserInfoTrace

package com.baozi;

import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

public class UserInfoTrace implements HttpSessionBindingListener {
    private String user;
    private UserInfo uinf=UserInfo.getInstance();
    public void setUser(String user){
        this.user=user;
    }
    public String getUser(){
        return user;
    }
    public void valueBound(HttpSessionBindingEvent arg0) {
        System.out.println("上线"+user);
    }
    public void valueUnbound(HttpSessionBindingEvent arg0) {
        System.out.println("下线"+user);
        if(user!="")
            uinf.removeUserInfo(user);
            
    }
}

index.jsp

<%@ page language="java" contentType="text/html; charset=gb2312"
    pageEncoding="gb2312" isErrorPage="true"%>
<%request.setCharacterEncoding("gb2312"); %>
<%@page import="com.baozi.*" %>




问卷调查


用户名

showuser.jsp

<%@ page language="java" contentType="text/html; charset=gb2312"
    pageEncoding="gb2312"%>

<%@ page import="java.util.*" %>
<%@ page import="com.baozi.*" %>



在线信息

<%
session.setMaxInactiveInterval(10);
UserInfo list=UserInfo.getInstance();
UserInfoTrace ut=new UserInfoTrace();
String name=request.getParameter("user");
ut.setUser(name);
session.setAttribute("list",ut);
list.addUserInfo(ut.getUser());
%>



你可能感兴趣的:(Servlet监听器统计在线人数)