文章目录
Filter过滤器
基本概念
Filter本意为”过滤“的含义,是JavaWeb的三大组件之一,三大组件:Servlet、Filter、Listener。
过滤器是向 Web 应用程序的请求和响应处理添加功能的 Web 服务组件。
过滤器相当于浏览器与Web资源之间的一道过滤网,在访问资源之前通过一系列的过滤器对请求进行修改、判断以及拦截等,也可以对响应进行修改、判断以及拦截等。
工作方式
Filter接口
- 基本概念:
javax.servlet.Filter
接口主要用于描述过滤器对象,可以对资源的请求和资源的响应操作进行筛选操作。
常用方法 | 功能介绍 |
---|---|
void init(FilterConfig filterConfig) | 初始化过滤器 |
void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) | 执行过滤操作 |
void destroy() | 销毁过滤器 |
FilterConfig接口
- 基本概念:
javax.servlet.FilterConfig
接口主要用于描述过滤器的配置信息。
常用方法 | 功能介绍 |
---|---|
String getFilterName() | 获取过滤器名 |
String getInitParameter(String name) | 获取指定的初始化参数信息 |
Enumeration getInitParameterNames() | 获取所有的初始化操作名称 |
ServletContext getServletContext() | 获取ServletContext 对象 |
多个过滤器的使用
- 如果存在多个过滤器都满足过滤条件,则容器依据映射的先后顺序来调用各个过滤器。
过滤器的优点
- 实现代码的“可插拔性”,即增加或减少某个功能模块,不会影响程序的正常执行。
- 可以将多个相同处理逻辑的模块集中写在过滤器里面,可实现重复利用、也方便代码的维护。
Listener监听器
基本概念
- Servlet规范中定义的一种特殊的组件,用来监听Servlet容器产生的事件并进行相应的处理。
- 容器产生的事件分类如下:
- 生命周期相关的事件。
- 属性状态相关的事件。
- 存值状态相关的事件。
- 底层原理是采用接口回调的方式实现。
各类监听器详解
ServletRequestListener
- 触发时机:
ServletRequest
创建和关闭时
常用方法 | 功能介绍 |
---|---|
void requestInitialized(ServletRequestEvent sre) | 实现ServletRequest 对象的初始化 |
void requestDestroyed(ServletRequestEvent sre) | 实现ServletRequest 对象的销毁 |
ServletRequestAttributeListener
- 触发时机:向
ServletRequest
添加、删除或替换一个属性时
常用方法 | 功能介绍 |
---|---|
void attributeAdded(ServletRequestAttributeEvent srae) | 增加属性时触发 |
void attributeReplaced(ServletRequestAttributeEvent srae) | 修改属性时触发 |
void attributeRemoved(ServletRequestAttributeEvent srae) | 删除属性时触发 |
HttpSessionListener
- 触发时机:当一个
HttpSession
刚被创建或失效(invalidate
)时
常用方法 | 功能介绍 |
---|---|
void sessionCreated(HttpSessionEvent se) | 当一个HttpSession 对象被创建时会调用这个方法 |
void sessionDestroyed(HttpSessionEvent se) | 当一个HttpSession 超时或调用HttpSession 的invalidate() 方法让它销毁时,将会调用这个方法 |
HttpSessionAttributeListener
- 触发时机:
HttpSession
中添加、删除或替换一个属性时
常用方法 | 功能介绍 |
---|---|
void attributeAdded(HttpSessionBindingEvent se) | 当往会话中加入一个属性的时候会调用这个方法 |
void attributeRemoved(HttpSessionBindingEvent se) | 当从会话中删除一个属性的时候会调用这个方法 |
void attributeReplaced(HttpSessionBindingEvent se) | 当改变会话中的属性的时候会调用这个方法 |
ServletContextListener
- 触发时机:
ServletContext
创建和关闭时
常用方法 | 功能介绍 |
---|---|
void contextInitialized(ServletContextEvent sce) | 当ServletContext 创建的时候,将会调用这个方法 |
void contextDestroyed(ServletContextEvent sce) | 当ServletContext 销毁的时候(例如关闭应用服务器或重新加载应用),将会调用这个方法 |
ServletContextAttributeListener
- 触发时机:向
ServletContext
添加、删除或替换一个属性时
常用方法 | 功能介绍 |
---|---|
void attributeAdded(ServletContextAttributeEvent scae) | 往ServletContext 中加入一个属性的时候触发 |
void attributeRemoved(ServletContextAttributeEvent scae) | 从ServletContext 中删除一个属性的时候触发 |
void attributeReplaced(ServletContextAttributeEvent scae) | 改变ServletContext 中属性的时候触发 |
HttpSessionBindingListener
- 触发时机:
HttpSession
中绑定和解除绑定时
常用方法 | 功能介绍 |
---|---|
void valueBound(HttpSessionBindingEvent event) | 有对象绑定时调用该方法 |
void valueUnbound(HttpSessionBindingEvent event) | 有对象解除绑定时调用该方法 |
HttpSessionActivationListener
- 触发时机:当有session数值的钝化和活化操作时
常用方法 | 功能介绍 |
---|---|
void sessionWillPassivate(HttpSessionEvent se) | 钝化操作时调用该方法 |
void sessionDidActivate(HttpSessionEvent se) | 活化操作时调用该方法 |