Javaweb监听器是一种特殊的类,用于监听Web应用程序中的事件和对象。它可以监听Web应用程序的启动和关闭事件,会话的创建和销毁事件,以及请求和响应的事件等。监听器可以在特定事件发生时执行一些特定的操作,比如记录日志、初始化资源、清理资源等。
常见的Javaweb监听器包括ServletContextListener、HttpSessionListener、ServletRequestListener等。这些监听器可以帮助开发人员更好地管理Web应用程序的生命周期和资源,提高应用程序的性能和稳定性。
目录
二.ServletContextAttributeListener
四.HttpSessionAttributeListener
六.ServletRequestAttributeListener
八. HttpSessionActivationListener
本文介绍几个最常用的监听器
一.ServletContextListener
1.作用:监听 ServletContext 创建或销毁(当我们 Web 应用启动时,就会创建 ServletContext), 即生命周期监听
2.相关方法
- void contextInitialized(ServletContextEvent sce) 创 建 Servletcontext 时触发
- void contextDestroyed(ServletContextEvent sce) 销毁 Servletcontext时触发
3.常用于 加载初始化的配置文件;比如 spring 的配置文件 ,任务调度(配合定时器Timer/TimerTask)
/**
* 1. 当一个类实现了 ServletContextListener
* 2. 该类就是一个监听器
* 3. 该类可以监听的事件 由该类实现的监听接口决定 ,比如 实现ServletContextListener
* , 则该类就可以监听 ServletContext对象的创建和销毁, 以此类推
* 4. HspServletContextListener 就是一个监听者
* 5. 当web应用启动时,就会产生 ServletContextEvent 事件, 会调用监听器的对应事件处理方法
* contextInitialized, 同时会传递 事件对象
* 6. 程序员可以通过 ServletContextEvent 事件对象,来获取需要的信息, 然后再进行业务处理
* 7. tomcat怎么知道这个监听器存在 ? 因为我们需要在web.xml中配置
*/
public class HspServletContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
ServletContext servletContext = servletContextEvent.getServletContext();
System.out.println("HspServletContextListener 监听到 " +
servletContext + " 被创建..");
//如果我们获取到ServletContext 对象..进行业务处理
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
ServletContext servletContext = servletContextEvent.getServletContext();
System.out.println("HspServletContextListener 监听到 " +
servletContext + " 被销毁..");
//比如可以对servletContext 数据进行处理, 或者日志的管理...
System.out.println("进行处理工作.....");
}
}
二.ServletContextAttributeListener
1.作用:监听ServletContext属性变化
2.相关方法
- void attributeAdded(ServletContextAttributeEvent event) 添加属性时调用
- void attributeReplaced(ServletContextAttributeEvent event) 替换属性时调用
- void attributeRemoved(ServletContextAttributeEvent event) 移除属性时调用
public class HspServletContextAttributeListener implements ServletContextAttributeListener {
@Override
public void attributeAdded(ServletContextAttributeEvent servletContextAttributeEvent) {
System.out.println("HspServletContextAttributeListener 监听到添加属性.."
+ servletContextAttributeEvent.getName() + "=" + servletContextAttributeEvent.getValue() );
}
@Override
public void attributeRemoved(ServletContextAttributeEvent servletContextAttributeEvent) {
System.out.println("HspServletContextAttributeListener 监听到删除属性.."
+ servletContextAttributeEvent.getName() + "=" + servletContextAttributeEvent.getValue() );
}
@Override
public void attributeReplaced(ServletContextAttributeEvent servletContextAttributeEvent) {
System.out.println("HspServletContextAttributeListener 监听到修改属性.."
+ servletContextAttributeEvent.getName() + "=" + servletContextAttributeEvent.getValue() );
}
}
三. HttpSessionListener
1.作用:监听Session创建和销毁,即生命周期监听
2.相关方法
- void sessionCreated(HttpSessionEvent se) 创建 session 时调用
- void sessionDestroyed(HttpSessionEvent se) 销毁 session 时调用
3.常用于监控用户上线离线
public class HspHttpSessionListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
HttpSession session = httpSessionEvent.getSession();
//当session创建时,我们给它设置一个生命周期 30s
session.setMaxInactiveInterval(30);
System.out.println("HspHttpSessionListener 监听到 session创建= " +
session.getId());
System.out.println("用户id=" + session.getId() + " 上线");
}
@Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
HttpSession session = httpSessionEvent.getSession();
System.out.println("HspHttpSessionListener 监听到 session销毁= " +
session.getId());
System.out.println("用户id=" + session.getId() + " 离线");
}
}
四.HttpSessionAttributeListener
1.作用:监听Session属性变化
2.相关方法
- void attributeAdded(ServletRequestAttributeEvent srae) 添加属性时
- void attributeReplaced(ServletRequestAttributeEvent srae) 替换属性时
- void attributeRemoved(ServletRequestAttributeEvent srae) 移除属性时
public class HspHttpSessionAttributeListener implements HttpSessionAttributeListener {
@Override
public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {
//HttpSession session = httpSessionBindingEvent.getSession();
System.out.println("HspHttpSessionAttributeListener 监听到session添加属性" +
httpSessionBindingEvent.getName() + "=" + httpSessionBindingEvent.getValue());
}
@Override
public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {
HttpSession session = httpSessionBindingEvent.getSession();
System.out.println("HspHttpSessionAttributeListener 监听到session删除属性" +
httpSessionBindingEvent.getName());
}
@Override
public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {
System.out.println("HspHttpSessionAttributeListener 监听到session修改属性" +
httpSessionBindingEvent.getName() + "=" + httpSessionBindingEvent.getValue());
}
}
五.ServletRequestListener
1.作用:监听Request创建和销毁
2.相关方法
- void requestInitialized(ServletRequestEvent sre) 创建 request 时
- void requestDestroyed(ServletRequestEvent sre) 销毁 request时
3.常用于监控某个IP访问网站的频率,日志记录,访问资源的情况
public class HspRequestListener implements ServletRequestListener {
@Override
public void requestInitialized(ServletRequestEvent servletRequestEvent) {
System.out.println("HspRequestListener 监听到 request对象创建");
ServletRequest servletRequest = servletRequestEvent.getServletRequest();
System.out.println("记录访问日志....");
System.out.println("访问IP= " + servletRequest.getRemoteAddr());
System.out.println("访问的资源= " + ((HttpServletRequest)servletRequest).getRequestURL());
}
@Override
public void requestDestroyed(ServletRequestEvent servletRequestEvent) {
System.out.println("HspRequestListener 监听到 request对象被销毁");
}
}
六.ServletRequestAttributeListener
1.作用:监听Request属性变化
2.相关方法
- void attributeAdded(ServletRequestAttributeEvent srae) 添加属性时
- void attributeReplaced(ServletRequestAttributeEvent srae) 替换属性时
- void attributeRemoved(ServletRequestAttributeEvent srae) 移除属性时
七. HttpSessionBindingListener
1.作用:用于监听HttpSession中属性绑定和解绑的事件的监听器。当一个对象被绑定到HttpSession中或者从HttpSession中解绑时,实现了HttpSessionBindingListener接口的类可以接收到通知,并执行相应的操作。
2.相关方法
- void valueBound(HttpSessionBindingEvent event):当一个对象被绑定到HttpSession中时调用。可以在这个方法中执行一些初始化操作或者记录日志等操作。
- void valueUnbound(HttpSessionBindingEvent event):当一个对象从HttpSession中解绑时调用。可以在这个方法中执行一些清理操作或者释放资源等操作。
八. HttpSessionActivationListener
1.作用:用于监听HttpSession的钝化和活化事件的监听器。当一个HttpSession被钝化(即序列化到磁盘上)或者被活化(即从磁盘上反序列化回内存)时,实现了HttpSessionActivationListener接口的类可以接收到通知,并执行相应的操作。
2.相关方法
- void sessionWillPassivate(HttpSessionEvent se):当一个HttpSession将要被钝化时调用。可以在这个方法中执行一些准备钝化的操作或者记录日志等操作。
- void sessionDidActivate(HttpSessionEvent se):当一个HttpSession被活化时调用。可以在这个方法中执行一些准备活化的操作或者恢复状态等操作。