java过滤器的概述
概述:Filter是用于过滤web资源的一个组件;在资源执行之前执行,在资源执行之后执行。
A filter is an object that performs filtering tasks on either the request to a resource (a servlet or static content), or on the response from a resource, or both.
使用场景:
- 登录状态认证
- 解决请求参数中文乱码
我们以案例驱动来学习下面就开始:
关于Filter的案例
开发步骤:
- 定义Filter类的实现Filter接口
-
重写doFilter方法
- 编写web.xml文件
-
配置定义Filter类
1-定义Filter类实现Filter接口
public class Demo01 implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("停车检查");
filterChain.doFilter(servletRequest, servletResponse);
System.out.println("放行");
}
}
2-编写web.xml配置文件
<filter>
<filter-name>Demo01</filter-name>
<filter-class>Filter.Demo01</filter-class>
</filter>
<filter-mapping>
<filter-name>Demo01</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3-测试类
@WebServlet("/Demo01")
public class Demo01Servlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("Demo01Servlet Over~");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
有关Filter的执行流程
- 执行流程
- 浏览器发起请求"http://localhost:8080/web12/demo01"
- 服务器处理请求,创建servletrequest和servletresponse对象,将这两个对象给Demo01
- Demo01Filter通过doFilter方法处理请求,将请求放行,将servletRequest和servletResponse对象给Demo01
- Demo01Servlet通过doGet/doPost方法处理请求,做出响应
- Demo01进行收尾。
[上面Filter的执行结果]
停车检查
Demo01Servlet
放行
停车检查
Demo01Servlet
放行
Filter的生命周期
初始化:随着服务器的启动而初始化 销毁:随着服务器的关闭而销毁
Filter的相关配置
初始化参数
<filter>
<filter-name>Demo03</filter-name>
<filter-class>com.atguigu.filter.Demo03</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Demo03</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>Demo02</filter-name>
<filter-class>Filter.Demo02</filter-class>
<init-param>
<param-name>name</param-name>
<param-value>name的值是我是你的</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Demo02</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Demo02.java
public class Demo02 implements Filter {
private String name ;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
name=filterConfig.getInitParameter("name");
System.out.println("初始化中... 输出name:"+name);
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
filterChain.doFilter(servletRequest,servletResponse);
System.out.println("过滤器中时输出name:"+name);
System.out.println("Demo02--doFilter执行完成!");
}
@Override
public void destroy() {
System.out.println("Demo02正在关闭资源...");
}
}
Demo03.java
public class Demo03 implements Filter {
private String encoding;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
encoding = filterConfig.getInitParameter("encoding");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("Demo03中的encoding = " + encoding);
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
System.out.println("Demo03中的资源正在关闭...");
}
}
Demo02与Demo03执行结果<执行是有顺序的 仔细看>
停车检查
Demo03中的encoding = utf-8
执行了Demo03Servlet
过滤器中时输出name:name的值是我是你的
Demo02--doFilter执行完成!
放行
过滤的路径
- 针对Servlet进行过滤
- 完全匹配 : 以"/"开头 目录匹配 :
- 以"/“开头,以”*"结尾 后缀名匹配 :
- 以"*"开头,以"后缀名"结尾
过滤链
概述:由多个过滤器组成的一个执行链。
<filter>
<filter-name>Demo05Filter</filter-name>
<filter-class>com.atguigu.filter.Demo05Filter</filter-class>
</filter>
<filter-mapping>
<filter-name>Demo05Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>Demo06Filter</filter-name>
<filter-class>com.atguigu.filter.Demo06Filter</filter-class>
</filter>
<filter-mapping>
<filter-name>Demo06Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Filter注解开发-
概述
@WebFilter(
filterName = "Demo08Filter",
initParams = {
@WebInitParam(name = "encoding",value = "utf-8")
},
servletNames = {
"Demo01Servlet"
},
urlPatterns = "/*",
dispatcherTypes = {
DispatcherType.REQUEST,
DispatcherType.FORWARD,
DispatcherType.INCLUDE,
DispatcherType.ERROR
}
)
public class Demo08Filter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("Demo08Filter");
chain.doFilter(request, response);
}
}
@WebFilter("/*")
public class Demo09Filter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
chain.doFilter(request, response);
}
}
有关Listener的案例以及讲解
Servlet监听器
- 事件源:三大域对象(ServletRequest、HttpSession、ServletContext)
- 监听器:三类监听器
- 事件:三大域对象发生改变
- 事件绑定:通过web.xml或注解来绑定
- 分类
- 一类监听器
- 二类监听器
- 三类监听器
- 监听域对象中的对象状态改变(绑定/解绑、钝化/活化)
一类监听器
- 概述
- 分类
- ServletRequestListener
- HttpSessionListener
- ServletContextListener
二类监听器
- 概述
- 分类
- ServletRequestAttributeListener
- HttpSessionAttributeListener
- ServletContextAttributeListener
三类监听器
- 概述
- 监听域对象中的javabean对象的状态改变(绑定/解绑、钝化/活化)
- 分类
- HttpSessionBindingListener
- 监听session对象中的javabean对象的绑定/解绑
- HttpSessionActivationListener
- 监听session对象中的javabean对象的钝化/活化
session钝化与活化
- session钝化
- 在服务器关闭时,将javabean对象信息存储到缓存文件
- session活化
- 在服务器重新启动时,将缓存文件中的对象信息读取出来设置到javabean对象中
|