HttpSessionListener有2個接口需要實現
sessionCreated //新建一個會話時候觸發也可以說是客戶端第一次和服務器交互時候觸發
sessionDestroyed //銷燬會話的時候 一般來說只有某個按鈕觸發進行銷燬 或者配置定時銷燬 ( 很多文獻中提到說瀏覽器關閉時候會銷燬 但是樓主通過各種現行主流瀏覽器測試效果不盡如人意)
HttpSessionAttributeListener有3個接口需要實現
attributeAdded //在session中添加對象時觸發此操作 籠統的說就是調用setAttribute這個方法時候會觸發的
attributeRemoved //修改、刪除session中添加對象時觸發此操作 籠統的說就是調用 removeAttribute這個方法時候會觸發的
attributeReplaced //在Session屬性被重新設置時
以下是一個統計在線會話數的功能,並且讓超時的自動銷燬
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>
- org.xiosu.listener.onlineListener
- </listener-class>
- </listener>
- <!--默認的會話超時時間間隔,以分鐘爲單位 -->
- <session-config>
- <session-timeout>1</session-timeout>
- </session-config>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
- <?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>
- org.xiosu.listener.onlineListener
- </listener-class>
- </listener>
- <!--默認的會話超時時間間隔,以分鐘爲單位 -->
- <session-config>
- <session-timeout>1</session-timeout>
- </session-config>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
onlineListener.java
- package org.xiosu.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");
- }
- }
- package org.xiosu.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");
- }
- }
index.jsp
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%
- 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>
- <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>
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%
- 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>
- <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+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'isOnline.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>
- <body>
- <%
- session=request.getSession();
- session.setAttribute("userName",request.getParameter("uName"));
- response.sendRedirect("showOnline.jsp");
- %>
- </body>
- </html>
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%
- 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 'isOnline.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>
- <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+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'showOnline.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>
- <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>