监听在线用户
監聽器的作用是監聽Web容器的有效事件,有Servlet容器管理,應用Listener接口監聽某個執行程序,根據該程序的需求做出適當的響應
本列應用HttpBindingListener監聽接口,監聽HTTP會話中對象的綁定信息。它是唯一不需要在web.xml中設定的監聽。
HttpBindingListener接口提供兩個方法: valueBound(HttpSessionBindingEvent arg0):當有對象加入session的范圍時會被自動調用 valueUnbound(HttpSessionBindingEvent arg0):當有對象從session的范圍內移除時會被自動調動
創建LoginList.java類來存放用戶和在線用戶的具體操作
創建LoginNote.java類,實現HttoSessionBindingListener類
public class LoginNote implements javax.servlet.http.HttpSessionBindingListener {private String user;private LoginList container=LoginList.getInstance();public LoginNote(){user="";}public void setUser(String user){this.user=user;}public String getUser(){return this.user;}@Overridepublic void valueBound(HttpSessionBindingEvent arg0) {System.out.println(this.user+"該用戶已經上線");}@Overridepublic void valueUnbound(HttpSessionBindingEvent arg0) {System.out.println(this.user+"該用戶已經下線");if(user!=""){container.removeLoginList(user);}}}創建LoginList.jsp在線用戶頁面
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@ page import="java.util.*" %><%@ page import="com.cn.zj.Filter.*" %> <!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><%LoginList list=LoginList.getInstance();LoginNote ut=new LoginNote();String name=request.getParameter("user");ut.setUser(name);session.setAttribute("list",ut);list.addLoginList(ut.getUser());session.setMaxInactiveInterval(10);%> <body> <div align="center"> <table width="400" height="150" border="0" cellpadding="0" cellspacing="0" bgcolor="lightblue"> <tr align="center"><td>用戶在線列表</td></tr><tr><td align="center"><br><textarea rows="5" cols="22"><%Vector vector=list.getList();if(vector!=null&&vector.size()>0){for(int i=0;i<vector.size();i++){out.println(vector.elementAt(i)+"已經登錄在線");}}%></textarea><br><br><a href="loginOut.jsp">返回</a></td></tr> </table> </div> </body> </html>index.jsp頁面
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>監聽查看在線用戶</title> </head> <script language="javascript"> function checkEmpty(form){ for(i=0;i<form.length;i++){ if(form.elements[i].value==""){ alert("表單信息不能為空"); return false; } } } </script> <link href="css/style.css" rel="stylesheet" type="text/css"> <body> <div align="center"><table width="400" height="150" border="0" cellpadding="0" cellspacing="0" bgcolor="lightblue"><Tr><td> </td></Tr><tr><td align="center"><form name="form" method="post" action="LoginList.jsp" onSubmit="return checkEmpty(form)"><input type="text" name="user"><br><br><input type="submit" name="Submit" value="登錄上線"></form></td></tr></table> </div> </body> </html>loginOut.jsp頁面
<%@ page contentType="text/html; charset=gb2312" language="java" errorPage="" %> <%session.invalidate();out.println("<script>parent.location.href='index.jsp';</script>"); %>總結
- 上一篇: 权限验证过滤器
- 下一篇: 通过正则表达式验证日期