实现在线用户列表显示、注销的Listener
实现一个对在线用户的监听:即监听用户的登录和注销两个操作。需要自定义一个Listener,实现ServletContextListener,HttpSessionListener和HttpSessionAttributeListener。
一、表示层:
1、用户登录表单Login.jsp
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>用户登录表单</title>
</head>
<body>
<%--表单,提交方式为post-,提交到DoGetDemo--%>
<form action="LoginConf.jsp" method="post">
用户名:<input type="text" name="username"/><br>
<input type="submit" value="登录"/>
<input type="reset" value="重置"/>
</form>
</body>
</html>
2、用户登录处理页:LoginConf.jsp
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>登录判断</title>
</head>
<body>
<center>
<%
String username = request.getParameter("username") ; //接受用户名参数
//登录判断省略
session.setAttribute("username",username);
%>
//跳转到用户列表页
<jsp:forward page="ShowAllUser.jsp"></jsp:forward>
</center>
</body>
</html>
3、用户列表显示页面:ShowAllUser.jsp
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<%@ page import="java.util.*"%>
<html>
<head>
<title>在线用户列表</title>
</head>
<body>
<%=session.getAttribute("username")%>
欢迎你的登录,你可以<a href="Logout.jsp">注销</a>。
<h3>在线用户列表</h3><hr>
<%
ArrayList<String> allUser = (ArrayList<String>)application.getAttribute("allUser");
Iterator<String> iter = allUser.iterator();
while(iter.hasNext()) {
out.println("★" + iter.next() + "★ ");
}
%>
</body>
</html>
4、用户注销页面Logout.jsp
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>注销</title>
</head>
<body>
<%
//注销时进行session销毁
session.invalidate() ;
%>
</body>
</html>
二、自定义Listener:OnlineUserListener
package listener;
import java.util.ArrayList;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class OnlineUserListener implements
ServletContextListener,HttpSessionListener,HttpSessionAttributeListener{
// 声明一个ServletContext对象
private ServletContext application = null ;
//ServletContext创建时调用该方法
public void contextInitialized(ServletContextEvent sce) {
//储存所用用户名
ArrayList<String> allUser = new ArrayList<String>();
//获得当前application对象
application = sce.getServletContext();
//设置到application范围
application.setAttribute("allUser", allUser);
}
//ServletContext销毁时调用该方法
public void contextDestroyed(ServletContextEvent sce) {
}
//session创建时调用该方法
public void sessionCreated(HttpSessionEvent se) {
}
//session销毁时调用该方法
public void sessionDestroyed(HttpSessionEvent se) {
//获得当前所有的用户
ArrayList<String> allUser = (ArrayList<String>) application.getAttribute("allUser");
//获得删除的用户
String user = (String)se.getSession().getAttribute("username");
//删除该用户
allUser.remove(user);
//重新设置到application范围中
application.setAttribute("allUser", allUser);
}
//session范围属性添加时调用
public void attributeAdded(HttpSessionBindingEvent se) {
//获得当前所有的用户
ArrayList<String> allUser = (ArrayList<String>) application.getAttribute("allUser");
//获得添加的用户
String user = (String) se.getValue();
//添加到所有用户中
allUser.add(user);
}
//session范围属性移除时调用
public void attributeRemoved(HttpSessionBindingEvent se) {
}
//session范围属性替换时调用
public void attributeReplaced(HttpSessionBindingEvent se) {
}
}
三、Listener配置
<listener>
<listener-class>listener.OnlineUserListener</listener-class>
</listener>