目录
ServletContextAttributeListener监听器
本文章属于后端全套笔记的第三部分
介绍
监听器是一组来自于Servlet规范下接口,共有8个接口。在Tomcat存在servlet-api.jar包
监听器接口需要由开发人员亲自实现,Http服务器提供jar包并没有对应的实现类
监听器的主要作用是:用于监控【作用域对象生命周期变化时刻】以及【作用域对象共享数据变化时刻】
待会儿我们会学习最重要的两个监听器:ServletContextListener、ServletContextAttributeListener
复习:作用域对象
在Servlet规范中,认为在服务端内存中可以在某些条件下为两个Servlet之间提供数据共享方案的对象,被称为【作用域对象】
需要注意的是:Servlet规范下作用域对象: Cookie是作为浏览器缓存而存在的,不是作用域对象
作用域对象有三个:ServletContext 全局作用域对象、HttpSession 会话作用域对象、HttpServletRequest 请求作用域对象
简介
作用:通过这个接口合法的监测 ServletContext全局作用域对象 被初始化时刻以及被销毁时刻
ServletContextListener接口实现类开发三大步骤
实现ServletContextListener接口
public class ListenerClass implements ServletContextListener
.
重写监听器监听事件处理方法:contextInitialized、contextDestroyed
在web.xml文件将监听器接口实现类注册到Http服务器
-
- <listener>
- <listener-class>监听器类listener-class>
- listener>
监听事件处理方法:
- public void contextInitlized(); //全局作用域对象被Http服务器初始化被调用
- public void contextDestory(); //在全局作用域对象被Http服务器销毁时候触发调用
示例

- public class ListenerClass implements ServletContextListener {
-
- @Override
- public void contextInitialized(ServletContextEvent sce) {
- System.out.println("全局作用域对象 ServletContext 被创建");
- }
-
- @Override
- public void contextDestroyed(ServletContextEvent sce) {
- System.out.println("全局作用域对象 ServletContext 被销毁");
- }
- }

- public class S1 extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
-
- }
- }

- "1.0" encoding="UTF-8"?>
- <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
- version="4.0">
-
- <servlet>
- <servlet-name>S1servlet-name>
- <servlet-class>S1servlet-class>
- servlet>
- <servlet-mapping>
- <servlet-name>S1servlet-name>
- <url-pattern>/S1url-pattern>
- servlet-mapping>
-
-
- <listener>
- <listener-class>ListenerClasslistener-class>
- listener>
-
- web-app>

简介
作用:通过这个接口合法的监测 ServletContext全局作用域对象 共享数据变化时刻
ServletContextAttributeListener接口实现类开发三大步骤
实现ServletContextListener接口
public class ListenerClass implements ServletContextAttributeListener
重写监听器监听事件处理方法:attributeAdded、attributeRemoved、attributeReplaced
在web.xml文件将监听器接口实现类注册到Http服务器
监听事件处理方法:
- public void contextAdd(); //在全局作用域对象添加共享数据
- public void contextReplaced(); //在全局作用域对象更新共享数据
- public void contextRemove(); //在全局作用域对象删除共享数据
示例

- public class ListenerClass2 implements ServletContextAttributeListener {
- @Override
- public void attributeAdded(ServletContextAttributeEvent scae) {
- System.out.println("检测到全局作用域对象数据有【新增】");
- }
-
- @Override
- public void attributeRemoved(ServletContextAttributeEvent scae) {
- System.out.println("检测到全局作用域对象数据有【删除】");
- }
-
- @Override
- public void attributeReplaced(ServletContextAttributeEvent scae) {
- System.out.println("检测到全局作用域对象数据有【修改】");
- }
- }

- public class S1 extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- ServletContext application = req.getServletContext();
- application.setAttribute("key1",100); //新增
- application.setAttribute("key1",200); //更新
- application.removeAttribute("key1"); //删除
- }
- }

- "1.0" encoding="UTF-8"?>
- <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
- version="4.0">
-
- <servlet>
- <servlet-name>S1servlet-name>
- <servlet-class>S1servlet-class>
- servlet>
- <servlet-mapping>
- <servlet-name>S1servlet-name>
- <url-pattern>/S1url-pattern>
- servlet-mapping>
-
-
- <listener>
- <listener-class>ListenerClass2listener-class>
- listener>
-
- web-app>

使用 ServletContextListener监听器,可在 contextInitlized( ) 方法内写上需要全局初始化的代码;可以在 contextDestroy( ) 方法内写上需要内存释放的代码
使用ServletContextAttributeListener监听器,可以监测数据变动,从而编写相应的行为
介绍
过滤器接口是来自于Servlet规范下接口,在Tomcat中存在于servlet-api.jar包
Filter接口实现类由开发人员负责编写,Http服务器不负责提供
Filter的作用:在Http服务器调用资源文件之前,对Http服务器进行拦截
具体作用
拦截Http服务器,帮助Http服务器检测当前请求合法性,若合法则放行
拦截Http服务器,对当前请求进行增强操作
Filter接口实现类开发步骤:三步
创建一个Java类实现Filter接口(注意:有很多个Filter,我们要实现的是Sevlet的Filter)
重写Filter接口中doFilter方法
- public class FilterClass implements Filter {
- @Override
- public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
-
- }
- }
3.web.xml将过滤器接口实现类注册到Http服务器
- <filter-mapping>
- <filter-name>oneFilterfilter-name>
- <url-pattern>拦截地址url-pattern>
- filter-mapping>
(拦截地址通知Tomcat在调用何种资源文件之前需要调用OneFilter过滤进行拦截) 拦截地址的写法有很多,下面列举一些:
- <url-pattern>/img/mm.jpgurl-pattern>
-
- <url-pattern>/img/*url-pattern>
-
- <url-pattern>*.jpgurl-pattern>
-
- <url-pattern>/*url-pattern>
要求Tomcat在调用【某一个具体文件】之前,来调用OneFilter拦截:获取用户信息,判断条件,若条件不符合则拒绝用户访问目标文件

要求Tomcat在调用【网站中任意文件】时,来调用OneFilter拦截:通知拦截的请求对象,使用UTF-8字符集进行编码(减少代码耦合度)
要求Tomcat在调用【网站中任意文件】时,来调用OneFilter拦截:防止恶意登录(没有HttpSession就不给登录,只能注册)
让与公共页面相关的页面无条件放行(即未登录、无HttpSession也能被访问)