HttpSessionBindingListener
HttpSessionBindingListener是Java 中对于Http协议的管理接口,如果一个类(对象)实现了该接口,那么当这个对象在被绑定,或者从Session中删除时,Servlet会通知这个对象,当接受到通知之后,则可以进行一些初始化或者清除。
如果一个对象实现了HttpSessionBindingListener接口,当这个对象被绑定到Session中或者从session中被删除时,Servlet容器会通知这个对象,而这个对象在接收到通知后,可以做一些初始化或清除状态的操作。
HttpSessionBindingListener API
javax.servlet.http.HttpSessionBindingListener接口提供了以下方法:
-
public void valueBound(HttpSessionBindingEvent event)
当对象正在被绑定到Session中,Servlet容器调用这个方法来通知该对象。 -
public void valueUnbound(HttpSessionBindingEvent event)
当从Session中删除对象时,Servlet容器调用这个方法来实现了HttpSessionBindingListener接口的对象,而这个对象 可以利用HttpSessionBindingEvent对象来访问与它相联系的HttpSession对象。
Javax.Servlet.http.HttpSessionBindingEvent类提供了以下两种方法:
-
public HttpSessionBindingEvent(HttpSession session,java.lang.String name)
-
public HttpSessionBindingEvent(HttpSession session,java.lang.string name,java.lang.Object value)
上面两个构造一个事件对象,当一个对象被绑定到Session中或者从Session中被删除时,用这个事件对象来通知它。 -
public java.lang.String getName()
返回绑定到Session中或者从session中删除的属性的名字。 -
public java.lang.Object getValue()
返回添加、删除或替换的属性的值。如果属性被添加或者被删除,这个方法返回属性的值。如果属性被替换,这个方法返回属性先前的值。 -
public HttpSession getSession()
返回HttpSession对象。
实例:在线人数统计
登录页面:index.jsp:
<%@ page contentType="text/html;charset=utf-8"%>
<html>
<head>
<title>test</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/login" method="post">
用户名:<input type="text" name="username" />
<br />
<input type="submit" value="登录" />
</form>
</body>
</html>
点击后跳转的LoginServlet:
/**
* @author zhang
* @date 2019/9/20 - 11:34
*/
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
// 取得登录的用户名
String username = request.getParameter("username");
//获得session
HttpSession session = request.getSession(true);
// 把用户名保存进session
session.setAttribute("username", username);
//获得ServletContext
ServletContext servletContext = getServletContext();
// 把用户名放入在线列表
List onlineUserList = (List) servletContext.getAttribute("onlineUserList");
// 第一次使用前,需要初始化
if (onlineUserList == null) {
onlineUserList = new ArrayList();
servletContext.setAttribute("onlineUserList", onlineUserList);
}
onlineUserList.add(username);
// 成功
response.sendRedirect(getServletContext().getContextPath() + "/result.jsp");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
登陆成功跳转到显示页面result.jsp:
<%@ page contentType="text/html;charset=utf-8"%>
<%@ page isELIgnored="false"%>
<%@page import="java.util.List"%>
<html>
<head>
<title>Title</title>
</head>
<body>
<h3>您好:${username} [<a href="logout.jsp">注销</a>]</h3>
当前在线用户:
<table>
<%
List onlineUserList = (List) application.getAttribute("onlineUserList");
for (int i = 0; i < onlineUserList.size(); i++) {
String onlineUsername = (String) onlineUserList.get(i);
%>
<tr>
<td><%=onlineUsername%></td>
</tr>
<%
}
%>
</table>
</body>
</html>
点击注销页面logout.jsp页面:
<%@ page contentType="text/html;charset=utf-8"%>
<%@ page import="java.util.*"%>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
// 取得登录的用户名
String username = (String) session.getAttribute("username");
// 销毁session
session.invalidate();
// 从在线列表中删除用户名
List onlineUserList = (List) application.getAttribute("onlineUserList");
onlineUserList.remove(username);
// 成功
response.sendRedirect(application.getContextPath() + "/index.jsp");
%>
</body>
</html>
监听器:HttpSessionListener
添加类OnlineUserListener,继承HttpSessionListener,HttpSessionListener中有两个方法sessionCreated(HttpSessionEvent event)与sessionDestroyed(HttpSessionEvent event),前者是监听session的新建,后者是监听session的销毁。
package pers.zhang.onLineCount;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.util.List;
/**
* @author zhang
* @date 2019/9/20 - 16:29
*/
public class OnLineUserListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
System.out.println("新建session:" + httpSessionEvent.getSession().getId());
}
@Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
HttpSession session = httpSessionEvent.getSession();
ServletContext servletContext = session.getServletContext();
//获得登录的用户名
String username = (String) session.getAttribute("username");
//从在线列表中删除用户名
List onlineUserList = (List) servletContext.getAttribute("onLineUserList");
System.out.println(username + "已经退出!");
}
}
配置:
<listener>
<listener-class>pers.zhang.onLineCount.OnLineUserListener</listener-class>
</listener>
一旦监听器发现调用了sessionDestoryed方法就会把其用户从在线人数中delete,在下面两种情况下会发生sessionDestoryed事件
- 执行session.invalidate()方法时
logout.jsp中调用了session.invalidate()方法 - session会话超时
session的默认超时时间是30分钟,30分钟后自动销毁session
HttpSessionBindingListener:
HttpSessionBindingListener虽然叫做监听器,但使用方法与HttpSessionListener完全不同。我们实际看一下它是如何使用的。
新建类OnlineUserBindingListener,实现HttpSessionBindingListener接口,构造方法传入username参数,HttpSessionBindingListener内有两个方法valueBound(HttpSessionBindingEvent event)和valueUnbound(HttpSessionBindingEvent event),前者为数据绑定,后者为取消绑定
所谓对session进行数据绑定,就是调用session.setAttribute()把HttpSessionBindingListener保存进session中。
这就是HttpSessionBindingListener和HttpSessionListener之间的最大区别:HttpSessionListener只需要设置到web.xml中就可以监听整个应用中的所有session。HttpSessionBindingListener必须实例化后放入某一个session中,才可以进行监听。
从监听范围上比较,HttpSessionListener设置一次就可以监听所有session,HttpSessionBindingListener通常都是一对一的
。
正是这种区别成就了HttpSessionBindingListener的优势,我们可以让每个listener对应一个username,这样就不需要每次再去session中读取username,进一步可以将所有操作在线列表的代码都移入listener,更容易维护。
package pers.zhang.onLineCount;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.util.List;
/**
* @author zhang
* @date 2019/9/20 - 16:29
*/
public class OnLineUserListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
System.out.println("新建session:" + httpSessionEvent.getSession().getId());
}
@Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
HttpSession session = httpSessionEvent.getSession();
ServletContext servletContext = session.getServletContext();
//获得登录的用户名
String username = (String) session.getAttribute("username");
//从在线列表中删除用户名
List onlineUserList = (List) servletContext.getAttribute("onLineUserList");
System.out.println(username + "已经退出!");
}
}
这里可以直接使用listener的username操作在线列表,不必再去担心session中是否存在username。
valueUnbound的触发条件是以下三种情况:
- 执行session.invalidate()时
- session超时,自动销毁时
- 执行session.setAttribute(“onLineUserListener”, “其它对象”);或session.removeAttribute(“onLineUserListener”);将listener从session中删除时。
因此,只要不将Listener从session中删除,就可以监听到session的销毁。
运行服务器,访问login.index:
点击登录,跳转后显示result.jsp:
再次访问login.jsp:
点击登录,跳转后显示result.jsp:
注销李四,并访问result.jsp:
待30分钟后,session超时销毁,张三用户将会被删除…