Java中HttpSessionListener监听器详解
HttpSessionListener接口是对Session的整体状态(创建,销毁)进行监听。
方法
//session创建 public void sessionCreated(HttpSessionEvent se); //session销毁 public void sessionDestroyed(HttpSessionEvent se); //HttpSessionEvent事件: //取得当前操作的session public HttpSession getSession();
例子
//onlineListener.java package test.listener; import java.util.ArrayList; import javax.servlet.ServletContext; import javax.servlet.http.HttpSessionAttributeListener; import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; public class onlineListener implements HttpSessionListener, HttpSessionAttributeListener { // 参数 ServletContext sc; ArrayList list = new ArrayList(); // 新建一个session时触发此操作 public void sessionCreated(HttpSessionEvent se) { sc = se.getSession().getServletContext(); System.out.println("新建一个session"); } // 销毁一个session时触发此操作 public void sessionDestroyed(HttpSessionEvent se) { System.out.println("销毁一个session"); if (!list.isEmpty()) { list.remove((String) se.getSession().getAttribute("userName")); sc.setAttribute("list", list); } } // 在session中添加对象时触发此操作,在list中添加一个对象 public void attributeAdded(HttpSessionBindingEvent sbe) { list.add((String) sbe.getValue()); System.out.println(sbe.getValue()); sc.setAttribute("list", list); } // 修改、删除session中添加对象时触发此操作 public void attributeRemoved(HttpSessionBindingEvent arg0) { System.out.println("5555555"); } public void attributeReplaced(HttpSessionBindingEvent arg0) { System.out.println("77777777"); } }
//web.xml配置监听器 <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <listener> <listener-class> test.listener.onlineListener </listener-class> </listener> <!--默认的会话超时时间间隔,以分钟为单位 --> <session-config> <session-timeout>1</session-timeout> </session-config> </web-app>
//index.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <html> <body> <% session = request.getSession(false); if (session != null) session.invalidate(); %> <form action="isOnline.jsp" method="post"> 用户名: <input type="text" name="uName" /> <input type="submit" value="上线"> </body> </html>
//isOnline.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <html> <body> <% session=request.getSession(); session.setAttribute("userName",request.getParameter("uName")); response.sendRedirect("showOnline.jsp"); %> </body> </html>
//showOnline.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <html> <body> <% ArrayList showList=(ArrayList)(getServletContext().getAttribute("list")); out.print("在线人数 "+showList.size()+"<br>"); for(int i=0;i<showList.size();i++){ out.print(showList.get(i)+"在线"+"<br>"); } %> <br> <a href="index.jsp">退出</a> </body> </html>
总结
HttpSessionListener中sessionCreated是新建一个会话时候触发也可以说是客户端第一次和服务器交互时候触发。 sessionDestroyed销毁会话的时候,一般来说只有某个按钮触发进行销毁或者配置定时销毁。
版权声明:本文为JAVASCHOOL原创文章,未经本站允许不得转载。