HttpSessionListener, HttpSessionAttributeListener的用法

来源:互联网 发布:天天向上网络女神 编辑:程序博客网 时间:2024/05/22 01:29
    

HttpSessionListener, HttpSessionAttributeListener的用法

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

    HttpSessionListener有2个接口需要实现

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

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

    HttpSessionAttributeListener有3个接口需要实现

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

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

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

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

    web.xml

    [html] view plain copy print?
    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>   


    onlineListener.java

    [java] view plain copy print?
    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. }  

    index.jsp

    [html] view plain copy print?
    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>  

    isOnline.jsp

    [html] view plain copy print?
    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>  

    showOnline.jsp

    [html] view plain copy print?
    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>  


    0 0
    原创粉丝点击