JavaWeb-Listener

--监听器的相关概念:

事件源:被监听的对象  -- 三个域对象 request  session  servletContext
监听器:监听事件源对象  事件源对象的状态的变化都会触发监听器 
注册监听器:将监听器与事件源进行绑定
响应行为:监听器监听到事件源的状态变化时 所涉及的功能代码 -- 程序员编写代码

 --配置

<!--监听器快速入门-->
<listener>
  <listener-class>com.itheima.a_quick.QuickListener</listener-class>
</listener>

注解:

@WebListener
public class QuickListener implements ServletContextListener {
    // 省略内容....
}

案例 在线用户:



@WebListener
public class ContextAsListener implements ServletContextListener,
        HttpSessionListener, HttpSessionAttributeListener, ServletRequestListener {

    // Public constructor is required by servlet spec
    public ContextAsListener() {
    }

    // -------------------------------------------------------
    // ServletContextListener implementation
    // -------------------------------------------------------
    public void contextInitialized(ServletContextEvent sce) {
      /* This method is called when the servlet context is
         initialized(when the Web application is deployed). 
         You can initialize servlet context related data here.
      */
        System.out.println("servletContext创建了");
        sce.getServletContext().setAttribute("num", 0);
    }

    public void contextDestroyed(ServletContextEvent sce) {
      /* This method is invoked when the Servlet Context 
         (the Web application) is undeployed or 
         Application Server shuts down.
      */
        System.out.println("servletContext销毁了");
    }

    // -------------------------------------------------------
    // HttpSessionListener implementation
    // -------------------------------------------------------
    public void sessionCreated(HttpSessionEvent se) {
        /* Session is created. */
        System.out.println("session创建");
        ServletContext servletContext = se.getSession().getServletContext();
        servletContext.setAttribute("num", (int) servletContext.getAttribute("num") + 1);
    }

    public void sessionDestroyed(HttpSessionEvent se) {
        /* Session is destroyed. */
        System.out.println("session销毁");
        ServletContext servletContext = se.getSession().getServletContext();
        servletContext.setAttribute("num", (int) servletContext.getAttribute("num") - 1);
    }

    // -------------------------------------------------------
    // HttpSessionAttributeListener implementation
    // -------------------------------------------------------

    public void attributeAdded(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute 
         is added to a session.
      */
    }

    public void attributeRemoved(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute
         is removed from a session.
      */
    }

    public void attributeReplaced(HttpSessionBindingEvent sbe) {
      /* This method is invoked when an attibute
         is replaced in a session.
      */
    }

    @Override
    public void requestDestroyed(ServletRequestEvent servletRequestEvent) {

    }

    @Override
    public void requestInitialized(ServletRequestEvent servletRequestEvent) {

    }
}

https://blog.csdn.net/qq_15204179/article/details/82055448

posted @ 2020-07-31 15:28  一半人生  阅读(130)  评论(0编辑  收藏  举报