本文主要是介绍Servlet监听器的简单使用实现统计在线人数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
随时随地技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)
下面用一个示例演示HttpSessionBindingListener监听器的使用:
package com.home.web.manager;import java.util.Vector;/*** * @description 用户管理* @author fj* @createTime 2017年2月27日*/
public class UserInfoList {private static UserInfoList user = new UserInfoList();private Vector<String> vector = null;public UserInfoList() {this.vector = new Vector<>();}public static UserInfoList getInstance() {return user;}public boolean addUserInfo(String user) {if (user != null) {vector.addElement(user);return true;} else {return false;}}public Vector<String> getList() {return vector;}public void removeUserInfo(String user) {if (user != null) {vector.removeElement(user);}}}
package com.home.web.manager;import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;/*** * @description 用来监听用户在线状态* @author fj* @createTime 2017年2月27日*/
public class UserInfoTrace implements HttpSessionBindingListener {private String user;private UserInfoList container = new UserInfoList();public UserInfoTrace() {user = "";}public String getUser() {return user;}public void setUser(String user) {this.user = user;}public UserInfoList getContainer() {return container;}public void setContainer(UserInfoList container) {this.container = container;}@Overridepublic void valueBound(HttpSessionBindingEvent arg0) {System.out.println(user + "上线了");}@Overridepublic void valueUnbound(HttpSessionBindingEvent arg0) {System.out.println(user + "下线了");if (user != "") {container.removeUserInfo(user);}}}
index.jsp:
<%@page import="java.net.URLDecoder"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!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>登录页</title>
</head>
<body><form action="result.jsp" method="post"><table align="center" width="350"><tr><td class="2" align="center" colspan="2"><h2>在线登录系统</h2></td></tr><tr><td align="center" colspan="2"><input type="text" name="user"></td></tr><tr><td class="2" align="center" colspan="2"><input type="submit"value="登录"></td></tr></table></form></body>
</html>
result.jsp:
<%@page import="com.home.web.manager.UserInfoTrace"%>
<%@page import="com.home.web.manager.UserInfoList"%>
<%@ page language="java" import="java.util.*" autoFlush="true"import="com.home.web.mamager.*" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!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><%String name = request.getParameter("user");UserInfoList list = UserInfoList.getInstance();UserInfoTrace ut = new UserInfoTrace();ut.setUser(name);session.setAttribute("list", ut);list.addUserInfo(ut.getUser());session.setMaxInactiveInterval(1);%><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></body>
</html>
这篇关于Servlet监听器的简单使用实现统计在线人数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!