<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[Heck's  Blog]]></title> 
<link>https://www.heckjj.com/index.php</link> 
<description><![CDATA[一瞬间的决定，往往可以改变很多，事实上，让自己成功的往往不是知识，是精神！ 如果你总是为自己找借口，那只好让成功推迟。执行力，今天！]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[Heck's  Blog]]></copyright>
<item>
<link>https://www.heckjj.com/post//</link>
<title><![CDATA[HttpSessionListener和HttpSessionAttributeListener的用法]]></title> 
<author>Heck &lt;@hecks.tk&gt;</author>
<category><![CDATA[Web开发]]></category>
<pubDate>Sun, 30 Mar 2014 07:43:31 +0000</pubDate> 
<guid>https://www.heckjj.com/post//</guid> 
<description>
<![CDATA[ 
	HttpSessionListener有2个接口需要实现<br/><br/>sessionCreated //新建一个会话时候触发也可以说是客户端第一次和服务器交互时候触发<br/><br/>sessionDestroyed //销毁会话的时候&nbsp;&nbsp;一般来说只有某个按钮触发进行销毁 或者配置定时销毁 （ 很多文献中提到说浏览器关闭时候会销毁 但是楼主通过各种现行主流浏览器测试效果不尽如人意）<br/>HttpSessionAttributeListener有3个接口需要实现<br/><br/>attributeAdded //在session中添加对象时触发此操作 笼统的说就是调用setAttribute这个方法时候会触发的<br/>attributeRemoved //修改、删除session中添加对象时触发此操作&nbsp;&nbsp;笼统的说就是调用 removeAttribute这个方法时候会触发的<br/>attributeReplaced //在Session属性被重新设置时<br/>以下是一个统计在线会话数的功能，并且让超时的自动销毁。<br/><br/>web.xml<br/><textarea name="code" class="xml" rows="15" cols="100">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;web-app version=&quot;2.4&quot; xmlns=&quot;http://java.sun.com/xml/ns/j2ee&quot;
&nbsp;&nbsp;xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
&nbsp;&nbsp;xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/j2ee 
&nbsp;&nbsp;http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd&quot;&gt;
&nbsp;&nbsp;&lt;listener&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;listener-class&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tk.hecks.listener.onlineListener
&nbsp;&nbsp;&nbsp;&nbsp;&lt;/listener-class&gt;
&nbsp;&nbsp;&lt;/listener&gt;


&nbsp;&nbsp;&lt;!--默认的会话超时时间间隔，以分钟为单位&nbsp;&nbsp;--&gt;
&nbsp;&nbsp;&lt;session-config&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;session-timeout&gt;1&lt;/session-timeout&gt;
&nbsp;&nbsp;&lt;/session-config&gt;
&nbsp;&nbsp;&lt;welcome-file-list&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
&nbsp;&nbsp;&lt;/welcome-file-list&gt;

&lt;/web-app&gt;
</textarea><br/><br/>onlineListener.java<br/><textarea name="code" class="java" rows="15" cols="100">
package tk.hecks.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,
&nbsp;&nbsp;&nbsp;&nbsp;HttpSessionAttributeListener &#123;
&nbsp;&nbsp;// 参数
&nbsp;&nbsp;ServletContext sc;

&nbsp;&nbsp;ArrayList list = new ArrayList();

&nbsp;&nbsp;// 新建一个session时触发此操作
&nbsp;&nbsp;public void sessionCreated(HttpSessionEvent se) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;sc = se.getSession().getServletContext();
&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&quot;新建一个session&quot;);
&nbsp;&nbsp;&#125;

&nbsp;&nbsp;// 销毁一个session时触发此操作
&nbsp;&nbsp;public void sessionDestroyed(HttpSessionEvent se) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&quot;销毁一个session&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;if (!list.isEmpty()) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list.remove((String) se.getSession().getAttribute(&quot;userName&quot;));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sc.setAttribute(&quot;list&quot;, list);
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&#125;

&nbsp;&nbsp;// 在session中添加对象时触发此操作，在list中添加一个对象
&nbsp;&nbsp;public void attributeAdded(HttpSessionBindingEvent sbe) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;list.add((String) sbe.getValue());
&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(sbe.getValue());
&nbsp;&nbsp;&nbsp;&nbsp;sc.setAttribute(&quot;list&quot;, list);
&nbsp;&nbsp;&#125;

&nbsp;&nbsp;// 修改、删除session中添加对象时触发此操作
&nbsp;&nbsp;public void attributeRemoved(HttpSessionBindingEvent arg0) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&quot;Attribute removed or deleted in session!&quot;);
&nbsp;&nbsp;&#125;

&nbsp;&nbsp;public void attributeReplaced(HttpSessionBindingEvent arg0) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&quot;Attribute replaced in session!&quot;);
&nbsp;&nbsp;&#125;
&#125;
</textarea><br/><br/>index.jsp<br/><textarea name="code" class="html" rows="15" cols="100">
&lt;%@ page language=&quot;java&quot; import=&quot;java.util.*&quot; pageEncoding=&quot;UTF-8&quot;%&gt;
&lt;%
&nbsp;&nbsp;String path = request.getContextPath();
&nbsp;&nbsp;String basePath = request.getScheme() + &quot;://&quot;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+ request.getServerName() + &quot;:&quot; + request.getServerPort()
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+ path + &quot;/&quot;;
%&gt;

&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;&gt;
&lt;html&gt;
&nbsp;&nbsp;&lt;head&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;base href=&quot;&lt;%=basePath%&gt;&quot;&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&lt;title&gt;My JSP &#039;index.jsp&#039; starting page&lt;/title&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;meta http-equiv=&quot;pragma&quot; content=&quot;no-cache&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;meta http-equiv=&quot;cache-control&quot; content=&quot;no-cache&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;meta http-equiv=&quot;expires&quot; content=&quot;0&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;meta http-equiv=&quot;keywords&quot; content=&quot;keyword1,keyword2,keyword3&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;meta http-equiv=&quot;description&quot; content=&quot;This is my page&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;!--
&nbsp;&nbsp;&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;styles.css&quot;&gt;
&nbsp;&nbsp;--&gt;
&nbsp;&nbsp;&lt;/head&gt;

&nbsp;&nbsp;&lt;body&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;%
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session = request.getSession(false);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (session != null)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session.invalidate();
&nbsp;&nbsp;&nbsp;&nbsp;%&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;form action=&quot;isOnline.jsp&quot; method=&quot;post&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;用户名：
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input type=&quot;text&quot; name=&quot;uName&quot; /&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input type=&quot;submit&quot; value=&quot;上线&quot;&gt;
&nbsp;&nbsp;&lt;/body&gt;
&lt;/html&gt;
</textarea><br/><br/>isOnline.jsp<br/><textarea name="code" class="html" rows="15" cols="100">
&lt;%@ page language=&quot;java&quot; import=&quot;java.util.*&quot; pageEncoding=&quot;UTF-8&quot;%&gt;
&lt;%
String path = request.getContextPath();
String basePath = request.getScheme()+&quot;://&quot;+request.getServerName()+&quot;:&quot;+request.getServerPort()+path+&quot;/&quot;;
%&gt;

&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;&gt;
&lt;html&gt;
&nbsp;&nbsp;&lt;head&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;base href=&quot;&lt;%=basePath%&gt;&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;title&gt;My JSP &#039;isOnline.jsp&#039; starting page&lt;/title&gt;
&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&lt;meta http-equiv=&quot;pragma&quot; content=&quot;no-cache&quot;&gt;
&nbsp;&nbsp;&lt;meta http-equiv=&quot;cache-control&quot; content=&quot;no-cache&quot;&gt;
&nbsp;&nbsp;&lt;meta http-equiv=&quot;expires&quot; content=&quot;0&quot;&gt;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&lt;meta http-equiv=&quot;keywords&quot; content=&quot;keyword1,keyword2,keyword3&quot;&gt;
&nbsp;&nbsp;&lt;meta http-equiv=&quot;description&quot; content=&quot;This is my page&quot;&gt;
&nbsp;&nbsp;&lt;!--
&nbsp;&nbsp;&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;styles.css&quot;&gt;
&nbsp;&nbsp;--&gt;

&nbsp;&nbsp;&lt;/head&gt;
&nbsp;&nbsp;
&nbsp;&nbsp;&lt;body&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;%

session=request.getSession();

session.setAttribute(&quot;userName&quot;,request.getParameter(&quot;uName&quot;));

response.sendRedirect(&quot;showOnline.jsp&quot;);
%&gt;
&nbsp;&nbsp;&lt;/body&gt;
&lt;/html&gt;
</textarea><br/><br/>showOnline.jsp<br/><textarea name="code" class="html" rows="15" cols="100">
&lt;%@ page language=&quot;java&quot; import=&quot;java.util.*&quot; pageEncoding=&quot;UTF-8&quot;%&gt;
&lt;%
String path = request.getContextPath();
String basePath = request.getScheme()+&quot;://&quot;+request.getServerName()+&quot;:&quot;+request.getServerPort()+path+&quot;/&quot;;
%&gt;

&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;&gt;
&lt;html&gt;
&nbsp;&nbsp;&lt;head&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;base href=&quot;&lt;%=basePath%&gt;&quot;&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&lt;title&gt;My JSP &#039;showOnline.jsp&#039; starting page&lt;/title&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&lt;meta http-equiv=&quot;pragma&quot; content=&quot;no-cache&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;meta http-equiv=&quot;cache-control&quot; content=&quot;no-cache&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;meta http-equiv=&quot;expires&quot; content=&quot;0&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;meta http-equiv=&quot;keywords&quot; content=&quot;keyword1,keyword2,keyword3&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;meta http-equiv=&quot;description&quot; content=&quot;This is my page&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;!--
&nbsp;&nbsp;&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;styles.css&quot;&gt;
&nbsp;&nbsp;--&gt;

&nbsp;&nbsp;&lt;/head&gt;

&nbsp;&nbsp;&lt;body&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;%
ArrayList showList=(ArrayList)(getServletContext().getAttribute(&quot;list&quot;));
out.print(&quot;在线人数 &quot;+showList.size()+&quot;&lt;br&gt;&quot;);
for(int i=0;i&lt;showList.size();i++)&#123;
out.print(showList.get(i)+&quot;在线&quot;+&quot;&lt;br&gt;&quot;);
&#125;
%&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;br&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;a href=&quot;index.jsp&quot;&gt;退出&lt;/a&gt;
&nbsp;&nbsp;&lt;/body&gt;
&lt;/html&gt;</textarea><br/>Tags - <a href="https://www.heckjj.com/tags/session/" rel="tag">session</a> , <a href="https://www.heckjj.com/tags/stylesheet/" rel="tag">stylesheet</a> , <a href="https://www.heckjj.com/tags/string/" rel="tag">string</a> , <a href="https://www.heckjj.com/tags/list/" rel="tag">list</a> , <a href="https://www.heckjj.com/tags/path/" rel="tag">path</a> , <a href="https://www.heckjj.com/tags/html/" rel="tag">html</a>
]]>
</description>
</item><item>
<link>https://www.heckjj.com/post//#blogcomment</link>
<title><![CDATA[[评论] HttpSessionListener和HttpSessionAttributeListener的用法]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>https://www.heckjj.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>