HttpSessionListener, HttpSessionAttributeListener的用法

468 篇文章 0 订阅
    

HttpSessionListener, HttpSessionAttributeListener的用法

7368人阅读 评论(1) 收藏 举报
本文章已收录于:
分类:

HttpSessionListener有2个接口需要实现

sessionCreated//新建一个会话时候触发也可以说是客户端第一次和服务器交互时候触发

sessionDestroyed//销毁会话的时候  一般来说只有某个按钮触发进行销毁 或者配置定时销毁 ( 很多文献中提到说浏览器关闭时候会销毁 但是楼主通过各种现行主流浏览器测试效果不尽如人意)

HttpSessionAttributeListener有3个接口需要实现

attributeAdded//在session中添加对象时触发此操作 笼统的说就是调用setAttribute这个方法时候会触发的

attributeRemoved//修改、删除session中添加对象时触发此操作  笼统的说就是调用 removeAttribute这个方法时候会触发的

attributeReplaced//在Session属性被重新设置时

以下是一个统计在线会话数的功能,并且让超时的自动销毁

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"    
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee     
  5.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">    
  6.     <listener>    
  7.         <listener-class>    
  8.             org.xiosu.listener.onlineListener  (实现session监听器接口的类的名字,包也要写上)  
  9.         </listener-class>    
  10.     </listener>    
  11.     
  12.     
  13.     <!--默认的会话超时时间间隔,以分钟为单位  -->    
  14.     <session-config>    
  15.         <session-timeout>1</session-timeout>    
  16.     </session-config>    
  17.     <welcome-file-list>    
  18.         <welcome-file>index.jsp</welcome-file>    
  19.     </welcome-file-list>    
  20.     
  21. </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  (实现session监听器接口的类的名字,包也要写上)
        </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

  1. package org.xiosu.listener;  
  2.   
  3. import java.util.ArrayList;  
  4. import javax.servlet.ServletContext;  
  5. import javax.servlet.http.HttpSessionAttributeListener;  
  6. import javax.servlet.http.HttpSessionBindingEvent;  
  7. import javax.servlet.http.HttpSessionEvent;  
  8. import javax.servlet.http.HttpSessionListener;  
  9.   
  10. public class onlineListener implements HttpSessionListener,  
  11.         HttpSessionAttributeListener {  
  12.     // 参数  
  13.     ServletContext sc;  
  14.   
  15.     ArrayList list = new ArrayList();  
  16.   
  17.     // 新建一个session时触发此操作  
  18.     public void sessionCreated(HttpSessionEvent se) {  
  19.         sc = se.getSession().getServletContext();  
  20.         System.out.println("新建一个session");  
  21.     }  
  22.   
  23.     // 销毁一个session时触发此操作  
  24.     public void sessionDestroyed(HttpSessionEvent se) {  
  25.         System.out.println("销毁一个session");  
  26.         if (!list.isEmpty()) {  
  27.             list.remove((String) se.getSession().getAttribute("userName"));  
  28.             sc.setAttribute("list", list);  
  29.         }  
  30.     }  
  31.   
  32.     // 在session中添加对象时触发此操作,在list中添加一个对象  
  33.     public void attributeAdded(HttpSessionBindingEvent sbe) {  
  34.         list.add((String) sbe.getValue());  
  35.         System.out.println(sbe.getValue());  
  36.         sc.setAttribute("list", list);  
  37.     }  
  38.   
  39.     // 修改、删除session中添加对象时触发此操作  
  40.     public void attributeRemoved(HttpSessionBindingEvent arg0) {  
  41.           
  42.         System.out.println("5555555");  
  43.     }  
  44.   
  45.     public void attributeReplaced(HttpSessionBindingEvent arg0) {  
  46.         System.out.println("77777777");  
  47.     }  
  48. }  
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

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3.     String path = request.getContextPath();  
  4.     String basePath = request.getScheme() + "://"  
  5.             + request.getServerName() + ":" + request.getServerPort()  
  6.             + path + "/";  
  7. %>  
  8.   
  9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  10. <html>  
  11.     <head>  
  12.         <base href="<%=basePath%>">  
  13.   
  14.         <title>My JSP 'index.jsp' starting page</title>  
  15.         <meta http-equiv="pragma" content="no-cache">  
  16.         <meta http-equiv="cache-control" content="no-cache">  
  17.         <meta http-equiv="expires" content="0">  
  18.         <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  19.         <meta http-equiv="description" content="This is my page">  
  20.         <!-- 
  21.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  22.     -->  
  23.     </head>  
  24.   
  25.     <body>  
  26.         <%  
  27.             session = request.getSession(false);  
  28.   
  29.             if (session != null)  
  30.                 session.invalidate();  
  31.         %>  
  32.         <form action="isOnline.jsp" method="post">  
  33.             用户名:  
  34.             <input type="text" name="uName" />  
  35.             <input type="submit" value="上线">  
  36.     </body>  
  37. </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

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'isOnline.jsp' starting page</title>  
  13.       
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <!-- 
  20.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  21.     -->  
  22.   
  23.   </head>  
  24.     
  25.   <body>  
  26.     <%  
  27.   
  28. session=request.getSession();  
  29.   
  30. session.setAttribute("userName",request.getParameter("uName"));  
  31.   
  32. response.sendRedirect("showOnline.jsp");  
  33. %>  
  34.   </body>  
  35. </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

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.     <head>  
  10.         <base href="<%=basePath%>">  
  11.   
  12.         <title>My JSP 'showOnline.jsp' starting page</title>  
  13.   
  14.         <meta http-equiv="pragma" content="no-cache">  
  15.         <meta http-equiv="cache-control" content="no-cache">  
  16.         <meta http-equiv="expires" content="0">  
  17.         <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.         <meta http-equiv="description" content="This is my page">  
  19.         <!-- 
  20.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  21.     -->  
  22.   
  23.     </head>  
  24.   
  25.     <body>  
  26.         <%  
  27. ArrayList showList=(ArrayList)(getServletContext().getAttribute("list"));  
  28. out.print("在线人数 "+showList.size()+"<br>");  
  29. for(int i=0;i<showList.size();i++){  
  30. out.print(showList.get(i)+"在线"+"<br>");  
  31. }  
  32. %>  
  33.         <br>  
  34.         <a href="index.jsp">退出</a>  
  35.     </body>  
  36. </html>  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值