Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 848|回复: 0

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

[复制链接]

该用户从未签到

发表于 2011-10-13 17:25:34 | 显示全部楼层 |阅读模式
前台
<%@ 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>
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

GMT+8, 2025-1-9 10:15 , Processed in 0.372779 second(s), 50 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表