Filter 过滤器
基本概念
Filter 本意为”过滤“的含义,是 JavaWeb 的三大组件之一,三大组件为:Servlet、Filter、Listener。
过滤器是向 Web 应用程序的请求和响应处理添加功能的 Web 服务组件。
过滤器相当于浏览器与 Web 资源之间的一道过滤网,在访问资源之前通过一系列的过滤器对请求进行修改、判断以及拦截等,也可以对响应进行修改、判断以及拦截等。
工作方式
浏览器发出请求,过滤器对请求进行“身份认证”、“资源审核”、“资源加密访问”等等功能,然后请求到达 Web 资源提供方。
Web 资源提供方对浏览器请求发出响应,过滤器也对响应进行过滤,然后响应到达浏览器。
使用方式
自定义类实现 Filter 接口并重写 doFilter 方法。
public class LoginFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException,ServletException {
// 处理逻辑,必须调用下面的方法放行
chain.doFilter(request,response);
}
}
在 web.xml 文件中配置过滤器。
<filter>
<filter-name>LoginFilter</filter-name>
<filter-class>com.zm.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LoginFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
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<String> getInitParameterNames() | 获取所有的初始化操作名称 |
ServletContext getServletContext() | 获取 ServletContext 对象 |
多个过滤器的使用
如果有多个过滤器都满足过滤的条件,则容器依据映射(filter-mapping)的先后顺序来调用各个过滤器,在浏览器和 Web 资源之间形成一条过滤器链。
过滤器优点
实现代码的“可插拔性/热插拔”,即增加或减少某个功能模块,不会影响程序的正常执行。
可以将多个相同处理逻辑的模块集中写在过滤器里面,可实现重复利用、也方便代码的维护.
Listener 监听器
基本概念
Servlet 规范中定义的一种特殊的组件,用来监听 Servlet 容器产生的事件并进行相应的处理。
容器产生的事件分类:
- 生命周期相关的事件。
- 属性状态相关的事件。
- 存值状态相关的事件。
底层原理是采用接口回调的方式实现。
基本分类
javax.servlet.ServletRequestListener -- 监听 request 作用域的创建和销毁
javax.servlet.ServletRequestAttributeListener -- 监听 request 作用域的属性状态变化
javax.servlet.http.HttpSessionListener -- 监听 session 作用域的创建和销毁
javax.servlet.http.HttpSessionAttributeListener -- 监听 session 作用域的属性状态变化
javax.servlet.ServletContextListener -- 监听 application 作用域的创建和销毁
javax.servlet.ServletContextAttributeListener -- 监听 application 作用域的属性状态变化
javax.servlet.http.HttpSessionBindingListener -- 监听对象与 session 的绑定和解除
javax.servlet.http.HttpSessionActivationListener -- 监听 session 数值的钝化和活化
监听器详解
-
ServletRequestListener
在 ServletRequest 创建和关闭时都会通知 ServletRequestListener 监听器。
需要在 web.xml 里面配置监听器。
方法声明 | 功能介绍 |
---|---|
void requestInitialized(ServletRequestEvent sre) | 实现 ServletRequest 对象的初始化 |
void requestDestroyed(ServletRequestEvent sre) | 实现 ServletRequest 对象的销毁 |
-
ServletRequestAttributeListener
向 ServletRequest 添加、删除或者替换一个属性的时候,将会通知 ServletRequestAttributeListener 监听器。
需要在 web.xml 里面配置监听器。
方法声明 | 功能介绍 |
---|---|
void attributeAdded(ServletRequestAttributeEvent srae) | 增加属性时触发 |
void attributeReplaced(ServletRequestAttributeEvent srae) | 修改属性时触发 |
void attributeRemoved(ServletRequestAttributeEvent srae) | 删除属性时触发 |
-
HttpSessionListener
当一个 HttpSession 刚被创建或者失效(invalidate)的时候,将会通知 HttpSessionListener 监听器。
需要在 web.xml 里面配置监听器。
方法声明 | 功能介绍 |
---|---|
void sessionCreated(HttpSessionEvent se) | 当一个 HttpSession 对象被创建时会调用这个方法 |
void sessionDestroyed(HttpSessionEvent se) | 当一个 HttpSession 超时或者调用 HttpSession 的 invalidate() 方法让它销毁时,将会调用这个方法 |
-
HttpSessionAttributeListener
HttpSession 中添加、删除或者替换一个属性的时候,将会通知 HttpSessionAttributeListener 监听器。
需要在 web.xml 里面配置监听器。
方法声明 | 功能介绍 |
---|---|
void attributeAdded(HttpSessionBindingEvent se) | 当往会话中加入一个属性的时候会调用这个方法 |
void attributeRemoved(HttpSessionBindingEvent se) | 当从会话中删除一个属性的时候会调用这个方法 |
void attributeReplaced(HttpSessionBindingEvent se) | 当改变会话中的属性的时候会调用这个方法 |
-
ServletContextListener
在 ServletContext 创建和关闭时都会通知 ServletContextListener 监听器。
需要在 web.xml 里面配置监听器。
方法声明 | 功能介绍 |
---|---|
void contextInitialized(ServletContextEvent sce) | 当 ServletContext 创建的时候,将会调用这个方法 |
void contextDestroyed(ServletContextEvent sce) | 当 ServletContext 销毁的时候(例如关闭应用服务器或者重新加载应用),将会调用这个方法 |
-
ServletContextAttributeListener
向 ServletContext 添加、删除或者替换一个属性的时候,将会通知 ServletContextAttributesListener 监听器。
需要在 web.xml 里面配置监听器。
方法声明 | 功能介绍 |
---|---|
void attributeAdded(ServletContextAttributeEvent scae) | 往 ServletContext 中加入一个属性的时候触发 |
void attributeRemoved(ServletContextAttributeEvent scae) | 从 ServletContext 中删除一个属性的时候触发 |
void attributeReplaced(ServletContextAttributeEvent scae) | 改变 ServletContext 中属性的时候触发 |
-
HttpSessionBindingListener
HttpSession 中绑定和解除绑定时,将会通知 HttpSessionListener 监听器。
需要被绑定监听的对象实现这个监听器,并且不需要在 web.xml 里面配置。
方法声明 | 功能介绍 |
---|---|
void valueBound(HttpSessionBindingEvent event) | 有对象绑定时调用该方法 |
void valueUnbound(HttpSessionBindingEvent event) | 有对象解除绑定时调用该方法 |
-
HttpSessionActivationListener
当有 session 数值的钝化(持久化,序列化)和活化(读取,反序列化)操作时,将会通知 HttpSessionActivationListener 监听器。
需要被钝化和活化监听的对象实现这个监听器和 Serializable,并且不需要在 web.xml 里面配置。
方法声明 | 功能介绍 |
---|---|
void sessionWillPassivate(HttpSessionEvent se) | 有钝化操作时调用该方法 |
void sessionDidActivate(HttpSessionEvent se) | 有活化操作时调用该方法 |
活化操作需要配置 context.xml 文件的 session 存储路径:
<Manager className="org.apache.catalina.session.PersistentManager"
saveOnRestart="true">
<!-- 配置文件存放的路径信息,可以自由指定 -->
<Store className="org.apache.catalina.session.FileStore" directory="D:\session"/>
</Manager>
实战案例
自定义类实现监听器接口并重写相关的方法:
public class OnlineUser implements HttpSessionListener, ServletContextListener {
// 声明一个 ServletContex 类型的引用负责作为全局对象来记录当前在线用户的数量,通过属性记录
private ServletContext servletContext = null;
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
servletContext = servletContextEvent.getServletContext();
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
servletContext = null;
}
@Override
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
System.out.println("有新用户上线了...");
Object count = servletContext.getAttribute("count");
// 若当前用户为第一个用户,则将全局对象中的属性值设置为1即可
if (null == count) {
servletContext.setAttribute("count", 1);
}
// 若当前用户不是第一个用户,则将全局对象中原有的数据取出来加 1 后再放回去
else {
Integer integer = (Integer)count;
integer++;
servletContext.setAttribute("count", integer);
}
System.out.println("当前在线用户数量为:" + servletContext.getAttribute("count"));
}
@Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
System.out.println("有用户已下线...");
// 减去一个用户
}
}
在 web.xml 中配置监听器:
<listener>
<listener-class> com.renda.listener.OnlineUser </listener-class>
</listener>
网友评论