- 初始化: web 服务器启动,就已经初始化了;随时等待过滤对象出现!
- chain: 链的意思
1.过滤中的所有代码,在过滤特定请求的时候都会被执行
2.必须要让过滤器继续通行
chain.doFilter( request,response ); - 销毁: web 服务器关闭的时候,过滤器被销毁
public class ShowFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("init...........");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=UTF-8");
System.out.println("执行前");
chain.doFilter(request,response);
System.out.println("执行后");
}
@Override
public void destroy() {
System.out.println("destory..............");
}
}