监听器: 主要是用来监听特定对象的创建或销毁、属性的变化的! 是一个实现特定接口的普通java类!
对象:
自己创建自己用 (不用监听)
别人创建自己用 (需要监听)
Servlet中哪些对象需要监听?
request / session / servletContext
分别对应的是request监听器、session相关监听器、servletContext监听器
监听器(listener)
监听器接口:
一、监听对象创建/销毁的监听器接口
Interface ServletRequestListener 监听request对象的创建或销毁
Interface HttpSessionListener 监听session对象的创建或销毁
Interface ServletContextListener 监听servletContext对象的创建或销毁
二、监听对象属性的变化
Interface ServletRequestAttributeListener 监听request对象属性变化: 添加、移除、修改
Interface HttpSessionAttributeListener 监听session对象属性变化: 添加、移除、修改
Interface ServletContextAttributeListener 监听servletContext对象属性变化
三、session相关监听器
Interface HttpSessionBindingListener 监听对象绑定到session上的事件
Interface HttpSessionActivationListener(了解) 监听session序列化及反序列化的事件
404(路径写错)
500(服务器错误,调试)
package com.loaderman.demo.a_life; import javax.servlet.ServletRequestEvent; import javax.servlet.ServletRequestListener; /** * 监听request对象的创建或销毁 * @author Jie.Yuan * */ public class MyRequestListener implements ServletRequestListener{ // 对象销毁 @Override public void requestDestroyed(ServletRequestEvent sre) { // 获取request中存放的数据 Object obj = sre.getServletRequest().getAttribute("cn"); System.out.println(obj); System.out.println("MyRequestListener.requestDestroyed()"); } // 对象创建 @Override public void requestInitialized(ServletRequestEvent sre) { System.out.println("MyRequestListener.requestInitialized()"); } }
package com.loaderman.demo.a_life; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; /** * 监听ServletContext对象创建或销毁 * @author Jie.Yuan * */ public class MyServletContextListener implements ServletContextListener{ @Override public void contextDestroyed(ServletContextEvent sce) { System.out.println("MyServletContextListener.contextDestroyed()"); } @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("1..........MyServletContextListener.contextInitialized()"); } }
package com.loaderman.demo.a_life; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; /** * 监听Session对象创建、销毁 * @author Jie.Yuan * */ public class MySessionListener implements HttpSessionListener{ // 创建 @Override public void sessionCreated(HttpSessionEvent se) { System.out.println("MySessionListener.sessionCreated()"); } // 销毁 @Override public void sessionDestroyed(HttpSessionEvent se) { System.out.println("MySessionListener.sessionDestroyed()"); } }
package com.loaderman.demo.b_attr; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionAttributeListener; import javax.servlet.http.HttpSessionBindingEvent; /** * 监听session对象属性的变化 * @author Jie.Yuan * */ public class MySessionAttrListener implements HttpSessionAttributeListener { // 属性添加 @Override public void attributeAdded(HttpSessionBindingEvent se) { // 先获取session对象 HttpSession session = se.getSession(); // 获取添加的属性 Object obj = session.getAttribute("userName"); // 测试 System.out.println("添加的属性:" + obj); } // 属性移除 @Override public void attributeRemoved(HttpSessionBindingEvent se) { System.out.println("属性移除"); } // 属性被替换 @Override public void attributeReplaced(HttpSessionBindingEvent se) { // 获取sesison对象 HttpSession session = se.getSession(); // 获取替换前的值 Object old = se.getValue(); System.out.println("原来的值:" + old); // 获取新值 Object obj_new = session.getAttribute("userName"); System.out.println("新值:" + obj_new); } }
package com.loaderman.demo.c_session; import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionBindingListener; /** * 监听此对象绑定到session上的过程,需要实现session特定接口 * @author Jie.Yuan * */ public class Admin implements HttpSessionBindingListener { private int id; private String name; public Admin() { super(); } public Admin(int id, String name) { super(); this.id = id; this.name = name; } // 构造函数 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } // 对象放入session @Override public void valueBound(HttpSessionBindingEvent event) { System.out.println("Admin对象已经放入session"); } // 对象从session中移除 @Override public void valueUnbound(HttpSessionBindingEvent event) { System.out.println("Admin对象从session中移除!"); } }
<!-- session的最大活跃时间 --> <session-config> <session-timeout>60</session-timeout> </session-config> <!-- 监听request对象创建、销毁 --> <listener> <listener-class>com.loaderman.demo.a_life.MyRequestListener</listener-class> </listener> <!-- 监听session对象创建、销毁 --> <listener> <listener-class>com.loaderman.demo.a_life.MySessionListener</listener-class> </listener> <!-- 监听servletContext对象创建、销毁 --> <listener> <listener-class>com.loaderman.demo.a_life.MyServletContextListener</listener-class> </listener> <!-- 属性监听器 --> <listener> <listener-class>com.loaderman.demo.b_attr.MySessionAttrListener</listener-class> </listener>