1.监听器:
监听器就是一个实现特定接口的普通java程序,这个程序专门用于监听另一个java对象的方法调用或属性改变,当被监听对象发生上述事件后,监听器某个方法将立即被执行。
2.Servle监听器:
监听的事件源分别为 ServletContext, HttpSession 和 ServletRequest 这三个域对象
3.监听器类型:
监听三个域对象创建和销毁的事件监听器
监听域对象中属性的增加和删除的事件监听器
监听绑定到HttpSession 域中的某个对象的状态的事件监听器。
4.监听servletContext域对象创建和销毁
ServletContextListener 接口用于监听 ServletContext 对象的创建和销毁事件。
当 ServletContext 对象被创建时,激发contextInitialized (ServletContextEvent sce)方法
当 ServletContext 对象被销毁时,激发contextDestroyed(ServletContextEvent sce)方法。
MyServletContextListener
public class MyServletContextListener implements ServletContextListener{
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println(arg0.getServletContext() + "被创建了");
}
public void contextInitialized(ServletContextEvent arg0) {
System.out.println(arg0.getServletContext() + "被销毁了");
}
}
Web.xml
<listener> <listener-class>cn.class3g.web.listener.MyServletContextListener
</listener-class>
</listener>
一个 web.xml 文件中可以配置多个 Servlet 事件监听器,web 服务器按照它们在web.xml 文件中的注册顺序来加载和注册这些 Serlvet 事件监听器。
5.监听HttpSession域对象创建和销毁
HttpSessionListener接口用于监听HttpSession的创建和销毁
创建一个Session时,sessionCreated(HttpSessionEvent se) 方法将会被调用。
销毁一个Session时,sessionDestroyed (HttpSessionEvent se) 方法将会被调用。
6.监听HttpRequest域对象创建和销毁
ServletRequestListener 接口用于监听ServletRequest 对象的创建和销毁。
Request 对象被创建时,监听器的requestInitialized方法将会被调用。
Request对象被销毁时,监听器的requestDestroyed方法将会被调用。
7.监听三个域对象属性变化
Servlet规范定义了监听 ServletContext, HttpSession,HttpServletRequest 这三个对象中的属性变更信息事件的监听器。
这三个监听器接口分别是ServletContextAttributeListener,HttpSessionAttributeListener ServletRequestAttributeListener
这三个接口中都定义了三个方法来处理被监听对象中的属性的增加,删除和替换的事件,同一个事件在这三个接口中对应的方法名称完全相同,只是接受的参数类型不同。
8. MyServletContextAttributeListener
public class MyServletContextAttributeListener implements
ServletContextAttributeListener {
public void attributeAdded(ServletContextAttributeEvent arg0) {
ServletContext context = arg0.getServletContext();
Object data = arg0.getValue();
System.out.println(context + "中加入了"+data);
}
public void attributeRemoved(ServletContextAttributeEvent arg0) {
ServletContext context = arg0.getServletContext();
Object data = arg0.getValue();
System.out.println(context + "中删除了"+data);
}
public void attributeReplaced(ServletContextAttributeEvent arg0) {
ServletContext context = arg0.getServletContext();
Object data = arg0.getValue();
System.out.println(context + "中更新了"+data);
}
}
Index.jsp
<%
application.setAttribute("param","xxx");
application.setAttribute("param","yyy");
application.removeAttribute("param");
%>
9. 感知 Session 绑定的事件监听器
保存在 Session 域中的对象可以有多种状态:
绑定到 Session 中;从 Session 域中解除绑定;
随 Session 对象持久化到一个存储设备中;
随 Session 对象从一个存储设备中恢复
HttpSessionBindingListener接口和HttpSessionActivationListener接口 ,实现这两个接口的类不需要 web.xml 文件中进行注册
HttpSessionBindingListener接口
实现了HttpSessionBindingListener接口的 JavaBean 对象可以感知自己被绑定到 Session 中和从 Session 中删除的事件
HttpSessionActivationListener接口
实现了HttpSessionActivationListener接口的 JavaBean 对象可以感知自己被活化和钝化的事件
10. 显示登陆用户列表,并实现踢人功能
在SessionAttributeListener中代码如下
public void attributeAdded(HttpSessionBindingEvent arg0){
Objectobj = arg0.getValue();
if(obj instanceof User){
HttpSessionsession = arg0.getSession();
ServletContextcontext = session.getServletContext();
Mapmap = (Map) context.getAttribute("map");
if(map ==null){//第一个用户登陆,此时还没有创建Map集合,需要创建之且放入ServletContext对象中
map= new HashMap();
context.setAttribute("map", map);
}
Useruser = (User) obj;
//登陆用户的用户名作为key, 登陆用户的session对象作为value
map.put(user.getUsername(), session);
}
}
在KickUserServlet中的doget()方法中代码如下
String username = request.getParameter("username");
Mapmap = (Map) this.getServletContext().getAttribute("map");
HttpSessionsession = (HttpSession) map.get(username);
if(session !=null){ //开踢
session.invalidate();
map.remove(username);
} request.getRequestDispatcher("/listUser.jsp").forward(request, response);
在listUser.jsp中代码如下
当前的登陆用户有:<br/>
<c:forEach var="me" items="${map }">
${me.key }
<a href="${pageContext.request.contextPath}/servlet/KickUserServlet?username=${me.key}">
踢死你
</a><br/><br/>
</c:forEach>