Javaweb监听器实例之统计在线人数
本文实例为大家分享了Javaweb统计在线人数示的具体代码,供大家参考,具体内容如下
1. 实现功能
统计在线人数
显示每个人的sessionId、Ip、第一次登录时间
2. 运用监听器种类
- HttpSession
- ServletRequest
3. 实现思想
- 每一个用户对应着一个session,所以在每次session被创建时,num++;
- 实现一个用户类(sessionID、IP、Date)
- 获取IP地址的方法需要用request,所以我们要实现一个ServletRequest监听器
- 每一次请求对应着一个用户的创建
- 将所有的用户放在一个用户链表中
- 将链表通过ServletRequestEvent.getServletContext().setAttribute设置成全局变量
- 在主页遍历链表读出用户信息
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>
以上是 Javaweb监听器实例之统计在线人数 的全部内容, 来源链接: utcz.com/z/352157.html