亲宝软件园·资讯

展开

Javaweb监听器实例之统计在线人数

人气:0

1. 实现功能

统计在线人数
显示每个人的sessionId、Ip、第一次登录时间

2. 运用监听器种类

3. 实现思想

4. 包目录

5. 代码实现

User

package entity;

public class User {
 private String sessionID;
 private String ip;
 private String firstTime;

 public String getSessionID() {
 return sessionID;
 }

 public void setSessionID(String sessionID) {
 this.sessionID = sessionID;
 }

 public String getIp() {
 return ip;
 }

 public void setIp(String ip) {
 this.ip = ip;
 }

 public String getFirstTime() {
 return firstTime;
 }

 public void setFirstTime(String firstTime) {
 this.firstTime = firstTime;
 }

}

MyHttpSessionListener

package listenner;

import java.util.ArrayList;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import entity.User;
import util.SessionUtil;

@WebListener
public class MyHttpSessionListener implements HttpSessionListener {

 private int num = 0;//统计人数

 public void sessionCreated(HttpSessionEvent se) {
 num++;
 se.getSession().getServletContext().setAttribute("number", num);
 }

 @SuppressWarnings("unchecked")
 public void sessionDestroyed(HttpSessionEvent se) {
 num--;
 se.getSession().getServletContext().setAttribute("number", num);

 //在此用户被销毁的时候,将链表中对应的用户对象删除
 ArrayList<User> userlist = (ArrayList<User>) se.getSession().getServletContext().getAttribute("userlist");
 if (SessionUtil.getUserBySessionID(userlist, se.getSession().getId()) != null) {
  userlist.remove(SessionUtil.getUserBySessionID(userlist, se.getSession().getId()));
 }
 }

}

MyServeltRequestListener

package listenner;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpServletRequest;

import entity.User;
import util.SessionUtil;

@WebListener
public class MyServeltRequestListener implements ServletRequestListener {
 @SuppressWarnings("unchecked")
 public void requestInitialized(ServletRequestEvent sre) {

 ArrayList<User> userlist=null; 
 //获取全局变量中的链表,若不存在,则创建一个新的链表
 userlist= (ArrayList<User>) sre.getServletContext().getAttribute("userlist");
 if (userlist == null) {
  userlist = new ArrayList<User>();
 }


 //获取request对象
 HttpServletRequest request = (HttpServletRequest) sre.getServletRequest();
 //获取sessionID
 String sessionID = request.getSession().getId();
 //通过sessionID在链表中查找对象,若没有此对象,则创建加入到链表中
 if (SessionUtil.getUserBySessionID(userlist, sessionID) == null) {
  User user = new User();
  user.setSessionID(sessionID);
  user.setFirstTime(new SimpleDateFormat("YYYY-MM-DD hh:mm:ss").format(new Date()));
  user.setIp(request.getRemoteAddr());
  userlist.add(user);
 }
 //将链表设置为全局变量
 sre.getServletContext().setAttribute("userlist", userlist);
 }
}

getUserBySessionID

package util;

import java.util.ArrayList;

import entity.User;

public class SessionUtil {

 //工具类,通过session在链表中找到相应的用户对象
 public static User getUserBySessionID(ArrayList<User> userlist, String sessionID) {
 for (int i = 0; i < userlist.size(); i++) {
  User user=userlist.get(i);
  if (user.getSessionID().equals(sessionID)){
  return user;
  }
 }
 return null;
 }
}

index.jsp

<%@page import="entity.User"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
</head>
<body>
 当前在线人数:${number}
 <hr>
 <%
 ArrayList<User> userlist = (ArrayList<User>) request.getServletContext().getAttribute("userlist");
 if (userlist != null) {
  for (int i = 0; i < userlist.size(); i++) {

  User user = userlist.get(i);
 %>
 sessionID:<%=user.getSessionID()%>
 <br>
 IP:<%=user.getIp()%>
 <br>
 FirstName:<%=user.getFirstTime()%>
 <hr>
 <%}}%>
</body>
</html>
您可能感兴趣的文章:

加载全部内容

相关教程
猜你喜欢
用户评论