Javaweb基础--->利用监听器统计在线用户数量和用户信息

首页布局:index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ page import="com.hanqi.model.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
当前在线人数: <%=application.getAttribute("userCount") %>
<hr>
在线用户列表:<br>
<%
List<User> userList = (List<User>)application.getAttribute("userList");
if(userList!=null) {
for(User u : userList) {
out.print(u+"<br>");
}
}
%>
</body>
</html>

创建用户信息:User

package com.hanqi.model;

import java.text.SimpleDateFormat;
import java.util.Date; public class User {
private String sessionid;
private Date firstTime; public String getSessionid() {
return sessionid;
} public void setSessionid(String sessionid) {
this.sessionid = sessionid;
} public Date getFirstTime() {
return firstTime;
} public void setFirstTime(Date firstTime) {
this.firstTime = firstTime;
} @Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((firstTime == null) ? 0 : firstTime.hashCode());
result = prime * result + ((sessionid == null) ? 0 : sessionid.hashCode());
return result;
} @Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (firstTime == null) {
if (other.firstTime != null)
return false;
} else if (!firstTime.equals(other.firstTime))
return false;
if (sessionid == null) {
if (other.sessionid != null)
return false;
} else if (!sessionid.equals(other.sessionid))
return false;
return true;
} @Override
public String toString() {
return "User [sessionid=" + sessionid + ", 最近登录时间="
+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(firstTime) + "]";
} }

编写监听器:TestSessionListener

package com.hanqi.listener;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator; import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener; import com.hanqi.model.User; /**
* Application Lifecycle Listener implementation class TestSessionListener
*
*/
@WebListener
public class TestSessionListener implements HttpSessionListener { private int userCount; private ArrayList<User> userList; /**
* Default constructor.
*/
public TestSessionListener() {
// TODO Auto-generated constructor stub
} /**
* @see HttpSessionListener#sessionCreated(HttpSessionEvent)
*/
public void sessionCreated(HttpSessionEvent sessionEvent) {
userCount++;
if(userList==null) {
userList = new ArrayList<User>();
}
HttpSession session = sessionEvent.getSession(); String sessionid = session.getId();
long stime = session.getCreationTime(); if(!checkUsersSession(sessionid, userList)) {
User u = new User();
u.setFirstTime(new Date(stime));
u.setSessionid(sessionid);
userList.add(u);
} session.getServletContext().setAttribute("userCount", userCount);
session.getServletContext().setAttribute("userList", userList);
} private boolean checkUsersSession(String sessionid, ArrayList<User> userList2) {
for(User u : userList2) {
if(u.getSessionid().equals(sessionid)) {
return true;
}
}
return false;
} /**
* @see HttpSessionListener#sessionDestroyed(HttpSessionEvent)
*/
public void sessionDestroyed(HttpSessionEvent sessionEvent) {
userCount--;
HttpSession session = sessionEvent.getSession(); String sessionid = session.getId();
System.out.println(sessionid + "的用户下线"); if(checkUsersSession(sessionid, userList)) {
Iterator<User> iter = userList.iterator();
while(iter.hasNext()) {
User u = iter.next();
if(u.getSessionid().equals(sessionid)) {
iter.remove();
}
}
}
session.getServletContext().setAttribute("userCount", userCount);
session.getServletContext().setAttribute("userList", userList);
} }

在web.xml文件中注册监听器

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0"> <listener>
<listener-class>com.hanqi.listener.TestSessionListener</listener-class>
</listener> <session-config>
<session-timeout>1</session-timeout>
</session-config> </web-app>
上一篇:IT技术的进化道路


下一篇:maomao的fft板子