Jsp   发布时间:2019-10-07  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了HttpSessionListener和HttpSessionBindingListener监听session的销毁大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

<h2 class="title" style="clear: both;">1. 使用httpSessionListener
<div class="cnblogs_code">

  OnlineUserListener httpSessionListener {
</span><span style="color: #0000ff;"&gt;public</span> <span style="color: #0000ff;"&gt;void</span><span style="color: #000000;"&gt; sessionCreated(httpSessionEvent event) {
}

</span><span style="color: #0000ff;"&gt;public</span> <span style="color: #0000ff;"&gt;void</span><span style="color: #000000;"&gt; sessionDestroyed(httpSessionEvent event) {
    httpSession session </span>=<span style="color: #000000;"&gt; event.getSession();
    ServletContext application </span>=<span style="color: #000000;"&gt; session.getServletContext();

    </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 取得登录的用户名</span>
    String username = (String) session.getAttribute("username"<span style="color: #000000;"&gt;);

    </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 从在线列表中删除用户名</span>
    List onlineUserList = (List) application.getAttribute("onlineUserList"<span style="color: #000000;"&gt;);
    onlineUserList.remove(userName);

    System.out.println(username </span>+ "超时退出。"<span style="color: #000000;"&gt;);
}

}

OnlineUserListener实现了httpSessionListener定义的两个方法:sessionCreated()和sessionDestroyed()。这两个方法可以监听到当前应用中session的创建和销毁情况。我们这里只用到sessionDestroyed()在session销毁时进行操作就可以。

httpSessionEvent中获得即将销毁的session,得到session中的用户名,并从在线列表中删除。最后一句向console打印一条信息,提示操作成功,这只是为了调试用,正常运行时删除即可。

为了让监听器发挥作用,我们将它添加到web.xml中:

>Anni.onlineUserListener>

以下两种情况下就会发生sessionDestoryed(会话销毁)事件:

1.执行session.invalidate()方法时。

既然LogoutServlet.java中执行session.invalidate()时,会触发sessionDestory()从在线用户列表中清除当前用户,我们就不必在LogoutServlet.java中对在线列表进行操作了,所以LogoutServlet.java的内容现在是这样。

httpServletrequest request,httpServletResponse responsE) { request.getSession().invalidate(); response.sendRedirect("index.jsp"); }

2.如果用户长时间没有访问服务器,超过了会话最大超时时间,服务器就会自动销毁超时的session。

会话超时时间可以在web.xml中进行设置,为了容易看到超时效果,我们将超时时间设置为最小值。

1

时间单位是一分钟,并且只能是整数,如果是零或负数,那么会话就永远不会超时。

title" style="clear: both;">2. 使用httpSessionBindingListener

httpSessionBindingListener然叫做监听器,但使用方法与httpSessionListener完全不同。我们实际看一下它是如何使用的。

我们的OnlineUserBindingListener实现了httpSessionBindingListener接口,接口中共定义了两个方法:valueBound()和valueUnbound(),分别对应数据绑定,和取消绑定两个事件。

所谓对session进行数据绑定,就是调用session.setAttribute()把httpSessionBindingListener保存进session中。我们在LoginServlet.java中进行这一步。

session.setAttribute("onlineUserBindingListener", OnlineUserBindingListener(userName));

这就是httpSessionBindingListener和httpSessionListener之间的最大区别:httpSessionListener只需要设置到web.xml中就可以监听整个应用中的所有session。httpSessionBindingListener必须实例化后放入某一个session中,才可以进行监听。

从监听范围上比较,httpSessionListener设置一次就可以监听所有session,httpSessionBindingListener通常都是一对一的。

正是这种区别成就了httpSessionBindingListener的优势,我们可以让每个listener对应一个username,这样就不需要每次再去session中读取username,进一步可以将所有操作在线列表的代码都移入listener,更容易维护。

valueBound()方法的代码如下:

httpSessionBindingEvent event) { httpSession session =); ServletContext application =ntext();
</span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 把用户名放入在线列表</span>
List onlineUserList = (List) application.getAttribute("onlineUserList"<span style="color: #000000;"&gt;);
</span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 第一次使用前,需要初始化</span>
<span style="color: #0000ff;"&gt;if</span> (onlineUserList == <span style="color: #0000ff;"&gt;null</span><span style="color: #000000;"&gt;) {
    onlineUserList </span>= <span style="color: #0000ff;"&gt;new</span><span style="color: #000000;"&gt; ArrayList();
    application.setAttribute(</span>"onlineUserList"<span style="color: #000000;"&gt;,onlineUserList);
}
onlineUserList.add(</span><span style="color: #0000ff;"&gt;this</span><span style="color: #000000;"&gt;.userName);

}

username已经通过构造方法传递给listener,在数据绑定时,可以直接把它放入用户列表。

与之对应的valueUnbound()方法,代码如下:

httpSessionBindingEvent event) { httpSession session =); ServletContext application =ntext();
</span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 从在线列表中删除用户名</span>
List onlineUserList = (List) application.getAttribute("onlineUserList"<span style="color: #000000;"&gt;);
onlineUserList.remove(</span><span style="color: #0000ff;"&gt;this</span><span style="color: #000000;"&gt;.userName);

System.out.println(</span><span style="color: #0000ff;"&gt;this</span>.username + "退出。"<span style="color: #000000;"&gt;);

}

这里可以直接使用listener的username操作在线列表,不必再去担心session中是否存在username。

valueUnbound的触发条件是以下三种情况:

orderedlist">
    orderedlist" type="1">
  1. 执行session.invalidate()时。

  2. session超时,自动销毁时。

  3. 执行session.setAttribute("onlineUserBindingListener","其他对象");或session.removeAttribute("onlineUserBindingListener");将listener从session中删除时。

因此,只要不将listener从session中删除,就可以监听到session的销毁。

 

特别感谢http://www.mossle.com/docs/jsp/html/jsp-ch-08.html

大佬总结

以上是大佬教程为你收集整理的HttpSessionListener和HttpSessionBindingListener监听session的销毁全部内容,希望文章能够帮你解决HttpSessionListener和HttpSessionBindingListener监听session的销毁所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: