您的位置:首页 > 移动开发

session、application事件监听器实现在线用户列表

2010-01-27 10:07 417 查看
前台

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">   
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<script type="text/javascript">
window.onunload =function(){
     
}
</script>
<body>
<form action="Login?zhang=login" method="post">
    <ul>
       <li>用户名:<input type="text" name="userName"/></li>
       <li><input type="submit" value="登陆"/></li>
       <li><a href="Login?zhang=logout">用户注销</a></li>
    </ul>
    <ul>
       <li>当前在线用户</li>
       <c:forEach items="${userList}" var="userList">
        <li>${userList }</li>
       </c:forEach>
    </ul>
   </form>
</body>
</html>

监听器

package com.greendog.jiantingqi;

import java.util.ArrayList;
import java.util.List;

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;
/**
* 在线用户列表
* @author zhq
*
*/
public class Online implements ServletContextListener,
   HttpSessionListener, HttpSessionAttributeListener {
//声明一个servletContext对象
ServletContext application=null;
//用户若登陆后,又输入用户登陆,则下面的变量派上用场了
String lastUserName;
public String getLastUserName() {
   return lastUserName;
}
public void setLastUserName(String lastUserName) {
   this.lastUserName = lastUserName;
}
public void contextDestroyed(ServletContextEvent arg0) {
   System.out.println("*****容摧毁");
}
public void contextInitialized(ServletContextEvent arg0) {
   application = arg0.getServletContext();
   application.setAttribute("userList", new ArrayList());
   System.out.println("*****初始化容器,向application对象中加入一个list对象");
}
public void attributeAdded(HttpSessionBindingEvent arg0) {
   List userList=(List)application.getAttribute("userList");
   //String userName=(String)arg0.getValue();
   String userName=(String)arg0.getSession().getAttribute("userName");
   userList.add(userName);
   application.setAttribute("userList", userList);
   System.out.println("*****用户成功登陆-->"+userName);
   this.setLastUserName(userName);
}
public void attributeRemoved(HttpSessionBindingEvent arg0) {
  
}
public void attributeReplaced(HttpSessionBindingEvent arg0) {  
   String userName=(String)arg0.getSession().getAttribute("userName");
   List userList=(List)application.getAttribute("userList");
   userList.remove(this.getLastUserName());
   userList.add(userName);
   application.setAttribute("userList", userList);
   System.out.println("*****用户重写用户名了,"+getLastUserName()+"改为了-->"+userName);
   this.setLastUserName(userName);
}
public void sessionCreated(HttpSessionEvent se) {
   System.out.println("*****建立新的session连接,ID="+se.getSession().getId());
}
public void sessionDestroyed(HttpSessionEvent se) {
   String userName=(String)se.getSession().getAttribute("userName");
   List userList=(List)application.getAttribute("userList");
   userList.remove(userName);
   application.setAttribute("userList", userList);
   System.out.println("*****用户退出,用户名"+userName);
}

}

servlet

package com.greendog.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class Login extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
   this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
   String zhang=request.getParameter("zhang");
   if("login".equals(zhang)){
    this.login(request, response);
   }else if("logout".equals(zhang)){
    this.logout(request, response);
   }
}
public void login(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
   String userName=request.getParameter("userName");
   HttpSession session=request.getSession();
   session.setAttribute("userName", userName);
   ServletContext application=getServletContext();
   List userList=(List)application.getAttribute("userList");  
   request.setAttribute("userList", userList);
   request.getRequestDispatcher("login.jsp").forward(request, response);
}
public void logout(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
   HttpSession session=request.getSession();
   session.invalidate();
   ServletContext application=getServletContext();
   List userList=(List)application.getAttribute("userList");  
   request.setAttribute("userList", userList);
   request.getRequestDispatcher("login.jsp").forward(request, response);
}
}

监听器在web.xml中的配置

<listener>
<listener-class>com.greendog.jiantingqi.Online</listener-class>
</listener>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息