likes
comments
collection
share

Listener 监听器介绍以及代码演示

作者站长头像
站长
· 阅读数 65

JavaWeb 三大组件之监听器 Listener

Listener 监听器介绍

1. Listener 监听器它是 JavaWeb 的三大组件之一。JavaWeb 的三大组件分别是:Servlet 程序、Listener 监听器、Filter 过滤器

2. Listener 是 JavaEE 的规范,就是接口

3. 监听器的作用是,监听某种变化(一般就是对象创建/销毁, 属性变化), 触发对应方法完成相应的任务

JavaWeb 的监听器

ServletContextListener 监听器

1. 作用:监听 ServletContext 创建或销毁(当我们 Web 应用启动时,就会创建 ServletContext),

即生命周期监听,应用场景

(1)加载初始化的配置文件;比如 spring 的配置文件

(2)任务调度(配合定时器 Timer/TimerTask)

相关方法

void contextInitialized(ServletContextEvent sce) 创建 Servletcontext 时触发

voidcontextDestroyed(ServletContextEvent sce) 销毁 Servletcontext

代码演示

 wyxServletContextListener

/**
 * 1. 当一个类实现了 ServletContextListener
 * 2. 该类就是一个监听器
 * 3. 该类可以监听的事件 由该类实现的监听接口决定 ,比如 实现ServletContextListener
 *    , 则该类就可以监听 ServletContext对象的创建和销毁, 以此类推
 * 4. wyxServletContextListener 就是一个监听者
 * 5. 当web应用启动时,就会产生 ServletContextEvent 事件, 会调用监听器的对应事件处理方法
 *    contextInitialized, 同时会传递 事件对象
 * 6. 程序员可以通过 ServletContextEvent 事件对象,来获取需要的信息, 然后再进行业务处理
 * 7. tomcat怎么知道这个监听器存在 ? 因为我们需要在web.xml中配置
 */
public class wyxServletContextListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        ServletContext servletContext = servletContextEvent.getServletContext();
        System.out.println("WyxServletContextListener 监听到 " +
                servletContext + " 被创建..");
        //如果我们获取到ServletContext 对象..进行业务处理

    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        ServletContext servletContext = servletContextEvent.getServletContext();
        System.out.println("WyxServletContextListener 监听到 " +
                servletContext + " 被销毁..");

        //比如可以对servletContext 数据进行处理, 或者日志的管理...
        System.out.println("进行处理工作.....");
    }
}

xml配置

    <listener>
        <listener-class>com.wyx.listener.WyxServletContextListener</listener-class>
    </listener>

ServletContextAttributeListener 监听器

1. 作用:监听 ServletContext 属性变化

相关方法

void attributeAdded(ServletContextAttributeEvent event) 添加属性时调用

void attributeReplaced(ServletContextAttributeEvent event) 替换属性时调用

void attributeRemoved(ServletContextAttributeEvent event) 移除属性时调用

代码示例

WyxServletContextAttributeListener.java

public class WyxServletContextAttributeListener implements ServletContextAttributeListener {
    @Override
    public void attributeAdded(ServletContextAttributeEvent servletContextAttributeEvent) {

        System.out.println("WyxServletContextAttributeListener 监听到添加属性.."
                + servletContextAttributeEvent.getName() + "=" + servletContextAttributeEvent.getValue() );
    }

    @Override
    public void attributeRemoved(ServletContextAttributeEvent servletContextAttributeEvent) {

        System.out.println("WyxServletContextAttributeListener 监听到删除属性.."
                + servletContextAttributeEvent.getName() + "=" + servletContextAttributeEvent.getValue() );
    }

    @Override
    public void attributeReplaced(ServletContextAttributeEvent servletContextAttributeEvent) {

        System.out.println("WyxServletContextAttributeListener 监听到修改属性.."
                + servletContextAttributeEvent.getName() + "=" + servletContextAttributeEvent.getValue() );

    }
}

TestServlet.java 

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class TestServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        getServletContext().setAttribute("k1","k1_value");
    }
}

xml配置

 <listener>
        <listener-class>com.wyx.listener.WyxHttpSessionAttributeListener</listener-class>
    </listener>

HttpSessionListener 监听器

作用:监听 Session 创建或销毁,即生命周期监听

2. 相关方法

void sessionCreated(HttpSessionEvent se)创建 session 时调用

void sessionDestroyed(HttpSessionEvent se)销毁 session 时调用

使用方法和前面一样, 可以用于监控用户上线,离线

 代码示例

public class WyxHttpSessionListener implements HttpSessionListener {
    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {
        HttpSession session = httpSessionEvent.getSession();
        //当session创建时,我们给它设置一个生命周期 30s

        session.setMaxInactiveInterval(30);
        System.out.println("WyxHttpSessionListener 监听到 session创建= " +
                session.getId());

        System.out.println("用户id=" + session.getId() + " 上线");
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {

        HttpSession session = httpSessionEvent.getSession();

        System.out.println("WyxHttpSessionListener 监听到 session销毁= " +
                session.getId());
        System.out.println("用户id=" + session.getId() + " 离线");
    }
}

xml配置与前面一样

HttpSessionAttributeListener 监听器

作用:监听 Session 属性的变化

2. 相关方法

void attributeAdded(ServletRequestAttributeEvent srae) 添加属性时

void attributeReplaced(ServletRequestAttributeEvent srae) 替换属性时

void attributeRemoved(ServletRequestAttributeEvent srae) 移除属性

  代码示例

public class WyxHttpSessionAttributeListener implements HttpSessionAttributeListener {
    @Override
    public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {
        //HttpSession session = httpSessionBindingEvent.getSession();

        System.out.println("WyxHttpSessionAttributeListener 监听到session添加属性" +
                httpSessionBindingEvent.getName() + "=" + httpSessionBindingEvent.getValue());
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {
        HttpSession session = httpSessionBindingEvent.getSession();

        System.out.println("WyxHttpSessionAttributeListener 监听到session删除属性" +
                httpSessionBindingEvent.getName());
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {

        System.out.println("WyxHttpSessionAttributeListener 监听到session修改属性" +
                httpSessionBindingEvent.getName() + "=" + httpSessionBindingEvent.getValue());
    }
}

xml配置与前面一样 

ServletRequestListener 监听器

1. ServletRequestListener 监听器

2. 作用:监听 Request 创建或销毁,即 Request 生命周期监听

相关方法

void requestInitialized(ServletRequestEvent sre) 创建 request 时

void requestDestroyed(ServletRequestEvent sre) 销毁 request 时

3. 可以用来监控, 某个 IP 访问我们网站的频率, 日志记录 ,访问资源的情况

  代码示例

public class WyxRequestListener implements ServletRequestListener {

    @Override
    public void requestInitialized(ServletRequestEvent servletRequestEvent) {
        System.out.println("WyxRequestListener 监听到 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(" WyxRequestListener 监听到 request对象被销毁");
    }


}

ServletRequestAttributeListener 监听器

作用:监听 Request 属性变化

2. 相关方法

void attributeAdded(ServletRequestAttributeEvent srae) 添加属性时

void attributeReplaced(ServletRequestAttributeEvent srae) 替换属性时

void attributeRemoved(ServletRequestAttributeEvent srae)移除属性时

代码示例

public class WyxServletContextAttributeListener implements ServletContextAttributeListener {
    @Override
    public void attributeAdded(ServletContextAttributeEvent servletContextAttributeEvent) {

        System.out.println("WyxServletContextAttributeL istener 监听到添加属性.."
                + servletContextAttributeEvent.getName() + "=" + servletContextAttributeEvent.getValue() );
    }

    @Override
    public void attributeRemoved(ServletContextAttributeEvent servletContextAttributeEvent) {

        System.out.println("WyxServletContextAttributeListener 监听到删除属性.."
                + servletContextAttributeEvent.getName() + "=" + servletContextAttributeEvent.getValue() );
    }

    @Override
    public void attributeReplaced(ServletContextAttributeEvent servletContextAttributeEvent) {

        System.out.println("WyxServletContextAttributeListener 监听到修改属性.."
                + servletContextAttributeEvent.getName() + "=" + servletContextAttributeEvent.getValue() );

    }
}

 HttpSessionBindingListener 感知监听器

HttpSessionActivationListener 感知监听器

不常用 了解即可

转载自:https://juejin.cn/post/7240444181327265853
评论
请登录