Listener监听器

监听器作用

  用于监听web应用中某些对象的创建、销毁、增加,修改, 删除等动作的发生,然后作出相应的响应处理。

当范围对象的状态发生变化的时候,服务器会自动调用监听器对象中的方法。

监听器分类

  按监听的对象划分:

  • ServletContext对象的生命周期监听器与属性操作监听器;
  • HttpSession对象的生命周期监听器与属性操作监听器;
  • ServletRequest对象的生命周期监听器与属性操作监听器;

ServletContext对象的生命周期监听器与属性操作监听器

 

当容器启动时,会创建ServletContext对象,容器关闭时,会销毁ServletContext对象

1)生命周期监听器

ServletContextListener接口定义了ServletContext对象生命周期的监听行为。

 

  •  void contextInitialized(ServletContextEvent sce)

 

    ServletContext对象创建之后会触发该监听方法,并将 ServletContext对象传递到该方法中。

 

  •  void contextDestroyed(ServletContextEvent sce)

 

    ServletContext对象在销毁之前会触发该监听方法,并将 ServletContext对象传递到该方法中。

 

创建监听器

复制代码
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * 当容器启动时,会创建ServletContext对象,容器关闭时,会销毁ServletContext对象
 */
public class ServletContextLifecycle implements ServletContextListener {
    /**
     * 监听ServletContext对象创建的监听方法
     * @param sce
     */
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("ServletConttext init......");
    }

    /**
     * 监听ServletContext对象销毁的监听方法
     * @param sce
     */
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("ServletContext destroy......");
    }
}
复制代码

web.xml中对监听器进行配置

<!--    对监听器进行配置-->
    <listener>
        <listener-class>www.listener.ServletContextLifecycle</listener-class>
    </listener>

 

2)属性操作监听器

ServletContextAttributeListener接口定义了对于ServletContext对象属性操作的监听行为。

  •   void attributeAdded(ServletContextAttributeEvent scae)

      向ServletContext对象中添加属性时会触发该监听方法,并将 ServletContext对象传递到该方法中。

      触发事件方法 :servletContext.setAttribute("key","value")。

  •   void attributeRemoved(ServletContextAttributeEvent scae)

      当从ServletContext对象中删除属性时会触发该监听方法,并将 ServletContext对象传递到该方法中。

      触发事件方法 :servletContext.removeAttribute("key")。

  •   void attributeReplaced(ServletContextAttributeEvent scae) 

      当从ServletContext对象中属性的值发生替换时会触发该监听方 法,并将ServletContext对象传递到该方法中。

      触发事件方法 :servletContext.setAttribute("key","value")添加相同key值时会触发

创建监听器

复制代码
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;

/**
 * ServletContext的属性操作监听器
 */

public class ServletContextAttr implements ServletContextAttributeListener {
    /**
     * 向ServletContext对象中添加属性时会触发该监听方法,并将ServletContext对象传递到该方法中。
     * @param scae
     */
    @Override
    public void attributeAdded(ServletContextAttributeEvent scae) {
        System.out.println(" start add..........");
        System.out.println(scae.getServletContext());
        System.out.println("name:"+scae.getName()+",value:"+scae.getValue());
        System.out.println(" end add..........");
    }

    /**
     * 当从ServletContext对象中删除属性时会触发该监听方法,并将ServletContext对象传递到该方法中。
     * @param scae
     */
    @Override
    public void attributeRemoved(ServletContextAttributeEvent scae) {
        System.out.println("start remove.........");
        System.out.println(scae.getServletContext());
        System.out.println("name:"+scae.getName()+",value:"+scae.getValue());
        System.out.println("end remove.........");
    }

    /**
     * 当从ServletContext对象中属性的值发生替换时会触发该监听方法,并将ServletContext对象传递到该方法中。
     * @param scae
     */
    @Override
    public void attributeReplaced(ServletContextAttributeEvent scae) {
        System.out.println("start replace........");
        System.out.println(scae.getServletContext());
        System.out.println("name:"+scae.getName()+",value:"+scae.getValue());
        System.out.println("end replace........");
    }
}
复制代码

在web.xml中添加监听器

<!--    多个Listener配置多个监听器-->
    <listener>
        <listener-class>www.listener.ServletContextAttr</listener-class>
    </listener>

测试ServletContext对象属性监听器

复制代码
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/attr")
public class ServletContextAttr extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext serCon=this.getServletContext();
        //向ServletContext对象添加属性时,会触发属性操作监听器的attributeAdded方法
        serCon.setAttribute("name","张三");
        //向ServletContext对象添加相同key的属性时,会触发属性操作监听器的attributeReplaced方法
        serCon.setAttribute("name","李四");
        //向ServletContext对象删除属性时,会触发属性操作监听器的attributeRemoved方法
        serCon.removeAttribute("name");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
复制代码

HttpSession对象的生命周期监听器与属性操作监听器

1)生命周期监听器

HttpSessionListener接口定义了HttpSession对象生命周期的监听行为。

  • void sessionCreated(HttpSessionEvent se)

    HttpSession对象创建后会触发该监听方法,并将已创建HttpSession对象传递到该方法中。

  • void sessionDestroyed(HttpSessionEvent se)

    HttpSession对象在销毁之前会触发该监听方法,并将要销毁的HttpSession对象传递到该方法中。

 

复制代码
/**
 * HttpSession生命周期监听器
 */
public class HttpSessiomLifecycle implements HttpSessionListener {
    @Override
    public void sessionCreated(HttpSessionEvent se) {
        System.out.println(se.getSession());
        System.out.println("creat.......");
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        System.out.println(se.getSession());
        System.out.println("destroy......");
    }
}
复制代码

 

 

2)属性操作监听器

HttpSessionAttributeListener接口定义了对于HttpSession对象属性操作的监听行为。

  • void attributeAdded(HttpSessionBindingEvent se)

    向HttpSession对象中添加属性时会触发该监听方法,并将 HttpSession对象传递到该方法中。

    触发事件的方法HttpSession.setAttribute("key","value")。

  • void attributeRemoved(HttpSessionBindingEvent se)

    当从HttpSession对象中删除属性时会触发该监听方法,并将 HttpSession对象传递到该方法中。

    触发事件方法 HttpSession.removeAttribute("key")。

  • void attributeReplaced(HttpSessionBindingEvent se)

    当从HttpSession对象中属性的值发生替换时会触发该监听方法,并将HttpSession对象传递到该方法中。

    触发事件的方法 HttpSession.setAttribute("key","value")。

创建监听器

复制代码
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

/**
 * HttpSession属性操作监听器
 */
public class HttpSessionAttr implements HttpSessionAttributeListener {
    /**
     *向HttpSession对象中添加属性时会触发该监听方法,并将 HttpSession对象传递到该方法中。
     * @param se
     */
    @Override
    public void attributeAdded(HttpSessionBindingEvent se) {
        System.out.println("add start");
        System.out.println(se.getSession());
        System.out.println("name:"+se.getName());
        System.out.println("value:"+se.getValue());
        System.out.println("add end");
    }

    /**
     *当从HttpSession对象中删除属性时会触发该监听方法,并将 HttpSession对象传递到该方法中。
     * @param se
     */
    @Override
    public void attributeRemoved(HttpSessionBindingEvent se) {
        System.out.println("remove start");
        System.out.println(se.getSession());
        System.out.println("name:"+se.getName());
        System.out.println("value:"+se.getValue());
        System.out.println("remove end");
    }

    /**
     *当从HttpSession对象中属性的值发生替换时会触发该监听方法,并将HttpSession对象传递到该方法中。
     * @param se
     */
    @Override
    public void attributeReplaced(HttpSessionBindingEvent se) {
        System.out.println("replace start");
        System.out.println(se.getSession());
        System.out.println("name:"+se.getName());
        System.out.println("value:"+se.getValue());
        System.out.println("replace end");
    }
}
复制代码

web.xml

    <listener>
        <listener-class>www.listener.HttpSessionAttr</listener-class>
    </listener>

创建servlet测试监听器

复制代码
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/sessionattr")
public class HttpSessionAttr extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session=req.getSession();
        session.setAttribute("name","张三");
        session.setAttribute("name","李四");
        session.removeAttribute("name");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
复制代码

 

 

 

 

 HttpServletRequest对象的生命周期监听器与属性操作监听器

1)生命周期监听器

ServletRequestListener接口定义了ServletRequest(是 HttpServletRequest接口的父接口类型)对象生命周期的监听行为。

 

 

 

  • void requestInitialized(ServletRequestEvent sre) 

    HttpServletRequest对象创建后会触发该监听方法,并将已创建 HttpServletRequest对象传递到该方法中。

  • void requestDestroyed(ServletRequestEvent sre)

    HttpServletRequest对象在销毁之前会触发该监听方法,并将要销 毁HttpServletRequest对象传递到该方法中。

2)属性操作监听器

ServletRequestAttributeListener接口定义了对于 HttpServletRequest对象属性操作的监听行为。

  • void attributeAdded(ServletRequestAttributeEvent srae)

    向HttpServletRequest对象中添加属性时会触发该监听方法,并将 HttpServletRequest对象传递到该方法中。

    触发事件的方法 HttpServletRequest.setAttribute("key","value")。

  • void attributeRemoved(ServletRequestAttributeEvent srae)

    当从HttpServletRequest对象中删除属性时会触发该监听方法,并 将HttpServletRequest对象传递到该方法中。

    触发事件方法 HttpServletRequest.removeAttribute("key")。

  • void attributeReplaced(ServletRequestAttributeEvent srae)

    当从HttpServletRequest对象中属性的值发生替换时会触发该监听方法,并将HttpServletRequest对象传递到该方法中。

    触发事件的方法HttpServletRequest.setAttribute("key","value")。

基于注解式开发监听器

在监听器上通过@WebListener注解替代web.xml中 Listener的配置。

注解式开发

@WebListener
public class HttpRequestLifecycle implements ServletRequestListener {
}

web.xml配置文件开发

    <listener>
        <listener-class>www.listener.HttpRequestLifecycle</listener-class>
    </listener>

 

posted @   chaplu  阅读(72)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示