JavaWeb之监听器Listener

监听器Listener

作为一名Android开发人员,对监听器那是再熟悉不过了。在Android开发中,可以说是充斥着各种监听器。
监听器实际上就是接口回调。

作用
在JavaWeb中Listener是Servlet规范定义的一种特殊类,主要用于监听3个作用域创建销毁,以及其属性变更

Servlet中的三个作用域分别为

  1. HttpServletRequest
  2. HttpSession
  3. ServletContext

注意pageContext表示jsp整个页面,不属于Servlet中的作用域

应用场景:

  • 统计在线人数
  • 页面访问量
  • 应用启动时做一些初始化工作等等

新建一个监听器

下面我们来新建一个Listener,来看看是什么样子的

这里写图片描述

这里我们勾上注解
这里写图片描述

来看看创建好的默认代码

package listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionBindingEvent;

@WebListener()
public class MyListener implements ServletContextListener,
        HttpSessionListener, HttpSessionAttributeListener {

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

    // -------------------------------------------------------
    // 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.
      */
    }

    public void contextDestroyed(ServletContextEvent sce) {
      /* This method is invoked when the Servlet Context 
         (the Web application) is undeployed or 
         Application Server shuts down.
      */
    }

    // -------------------------------------------------------
    // HttpSessionListener implementation
    // -------------------------------------------------------
    public void sessionCreated(HttpSessionEvent se) {
        /* Session is created. */
    }

    public void sessionDestroyed(HttpSessionEvent se) {
        /* Session is destroyed. */
    }

    // -------------------------------------------------------
    // 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.
      */
    }
}

可以看到默认实现了ServletContextListenerHttpSessionListener和**HttpSessionAttributeListener **3个接口,这3个接口是比较常用的,所以IDE自动帮我们实现了这些接口。

实际上3个作用域对应的监听器是不一样的

  1. HttpServletRequest作用域对应的监听器是ServletRequestListener(监听request的创建和销毁)ServletRequestAttributeListener(监听request中属性的变更)
  2. HttpSession作用域对应的监听器是HttpSessionListener(监听session的创建)HttpSessionAttributeListener(监听session中属性的变更)
  3. ServletContext作用域对应的监听器是ServletContextListener(监听servletContext的创建于销毁)ServletContextAttributeListener(servletContext中属性的变更)

其实这三个作用域对应监听器中的方法是差不多的,下面我们以ServletContext作用域的监听器为例。看看里面的方法什么时候执行。

新建一个ContextListener类,并实现其对应的接口,监听器的配置比较简单,就一个value属性

package listener;

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener(value = "servletContextListener")
public class ContextListener implements ServletContextListener, ServletContextAttributeListener {
    @Override
    public void attributeAdded(ServletContextAttributeEvent scae) {
        /*添加属性时调用*/
        System.out.println("新增的属性:"+scae.getName()+":"+scae.getValue());
    }

    @Override
    public void attributeRemoved(ServletContextAttributeEvent scae) {
        /*属性移除时调用*/
        System.out.println("移除的属性:"+scae.getName()+":"+scae.getValue());
    }

    @Override
    public void attributeReplaced(ServletContextAttributeEvent scae) {
        /*属性替换时调用(修改值)*/
        System.out.println("替换的属性:"+scae.getName()+":"+scae.getValue());
    }

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        /*servletContext创建时调用*/
        System.out.println("项目启动了");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        /*servletContext销毁时调用*/
        System.out.println("项目停止了");
    }
}

再新建一个jsp页面,在页面中对application对象中的属性做一些修改

<%--
  Created by IntelliJ IDEA.
  User: yzq
  Date: 2018/8/1
  Time: 11:01
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>servletContext</title>

    <%
        /*application实际上就是ServletContext的实例*/
    application.setAttribute("user","111");//新增属性
    application.setAttribute("user","yzq");//修改属性
    application.removeAttribute("user");//移除属性
    %>

</head>
<body>

</body>
</html>

下面我们来看看执行效果

这里写图片描述
这样我们就实现了对ServletContext作用域的监听了。其他两个作用域监听器的使用方式也是类似的。

猜你喜欢

转载自blog.csdn.net/yuzhiqiang_1993/article/details/81324953