如何在个人web项目中使用Servlet监听器?

编译软件:IntelliJ IDEA 2019.2.4 x64
操作系统:win10 x64 位 家庭版
服务器软件:apache-tomcat-8.5.27



一. Servlet监听器是什么?

用官方一点的说法来说,“它是基于Java Servlet 规范的对象,主要用于监听Web应用程序中的ServletContext,HttpSession 和HttpServletRequest等域对象的创建与销毁事件,以及监听这些域对象中的属性发生修改的事件”,非常精辟的表述。

但这些话可能对新手小白来说不是很理解。莫急,个人理解,它就像我们现实生活中的窃听器一样,对某些特定的群体进行窃听,基于监听目标的特定行为事件进行相应的反应,比如臭名昭著的美国棱镜门窃听事件。


二. Servlet监听器有哪些作用?

2.1 监听域对象的创建和销毁

2.1.1 ServletContextListener接口

作用:

监听ServletContext对象(全局上下文对象)的创建与销毁

方法名 作用
contextInitialized(ServletContextEvent sce) ServletContext创建时调用
contextDestroyed(ServletContextEvent sce) ServletContext销毁时调用

附注:

ServletContext对象是在 web项目被加载时(Tomcat服务器开机)创建,但在web项目卸载时销毁(Tomcat服务器关机)

2.1.2 HttpSessionListener接口

作用:

监听HttpSession对象(会话域对象)的创建与销毁

方法名 作用
sessionCreated(HttpSessionEvent hse) HttpSession对象创建时调用
sessionDestroyed(HttpSessionEvent hse) HttpSession对象销毁时调用

附注:

HttpSession对象是在第一次调用getSession方法时创建,但在客户端关闭,强制失效,达到最大空闲时间时自动失效等三类情形下销毁对象。

2.1.3 ServletRequestListener接口

作用:

监听ServletRequest对象(请求域对象)的创建与销毁

方法名 作用
requestInitialized(ServletRequestEvent sre) ServletRequest对象创建时调用
requestDestroyed(ServletRequestEvent sre) ServletRequest对象销毁时调用

附注:

ServletRequest对象是在有请求就创建,但在响应结束时就销毁。

2.2 监听域对象内共享数据的添加、修改、删除

2.2.1 ServletContextAttributeListener接口

作用:

监听ServletContext对象中属性的添加、移除和修改

方法名 作用
attributeAdded(ServletContextAttributeEvent scab) 向ServletContext中添加属性时调用
attributeRemoved(ServletContextAttributeEvent scab) 从ServletContext中移除属性时调用
attributeReplaced(ServletContextAttributeEvent scab) 当ServletContext中的属性被修改时调用

ServletContextAttributeEvent对象代表属性变化事件,它包含的方法如下:

方法名 作用
getName() 获取修改或添加的属性名
getValue() 获取被修改或添加的属性值
getServletContext() 获取ServletContext对象

2.2.2 HttpSessionAttributeListener接口

作用:

监听HttpSession对象中属性的添加、移除和修改

方法名 作用
attributeAdded(HttpSessionBindingEvent se) 向HttpSession中添加属性时调用
attributeRemoved(HttpSessionBindingEvent se) 从HttpSession中移除属性时调用
attributeReplaced(HttpSessionBindingEvent se) 当HttpSession中的属性被修改时调用

HttpSessionBindingEvent对象代表属性变化事件,它包含的方法如下:

方法名 作用
getName() 获取修改或添加的属性名
getValue() 获取被修改或添加的属性值
getSession() 获取触发事件的HttpSession对象

2.2.3 ServletRequestAttributeListener接口

作用:

监听ServletRequest对象中属性的添加、移除和修改

方法名 作用
attributeAdded(ServletRequestAttributeEvent srae) 向ServletRequest中添加属性时调用
attributeRemoved(ServletRequestAttributeEvent srae) 从ServletRequest中移除属性时调用
attributeReplaced(ServletRequestAttributeEvent srae) 当ServletRequest中的属性被修改时调用

ServletRequestAttributeEvent对象代表属性变化事件,它包含的方法如下:

方法名 作用
getName() 获取修改或添加的属性名
getValue() 获取被修改或添加的属性值
getServletRequest () 获取触发事件的ServletRequest对象

三. 怎样创建一个Servlet监听器?

步骤:

①创建一个类

②实现一个接口

根据业务场景需要可参选实现三者(ServletContextListener,HttpSessionListener,或ServletRequestListener)之一的接口使用

③实现接口中的抽象方法

④注册监听器(最原始的做法)

位置:

在web.xml中注册

代码示例如下:

//注册刚创建的监听器MyListener
<listener>
    <listener-class>Listenner.MyListener</listener-class>
</listener>

四. 练手案例

案例:创建监听器MyListener类,继承ServletContextListener接口,用于监听全局上下文对象的创建与销毁情况,并在web.xml中注册该监听器。演示监听器观察全局上下文对象的创建与销毁。

①创建MyListener类,继承ServletContextListener接口

代码示例如下:

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

//监听ServletContext应用域对象
public class MyListener implements ServletContextListener {
    
    
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
    
    
        System.out.println("ServletContext对象创建后执行");
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
    
    
        System.out.println("ServletContext对象销毁后执行");
    }
}

②在web.xml里注册该监听器

代码示例如下:

<listener>
    <listener-class>Listener.MyListener</listener-class>
</listener>

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/siaok/article/details/130352741