过滤器简介
-
过滤器是一个用于
拦截数据源和数据目的地之间消息
的一个对象
-
过滤器功能:
1)分析请求
,将请求发送给指定的资源或自己创建一个响应返回(拦截请求
)
2)在请求到达服务器端前处理请求,设置请求信息,将请求封装成符合规则的对象(响应的预处理
)
3)在响应到达客户端前处理响应,将响应封装成符合规则的对象(响应的预处理
) -
在应用程序的生命周期:filter对象只创建一次,init,destroy,只执行一次;doFilter()执行多次,从应用程序的启动到关闭。
-
应用场合:字符类型的设置,图片的水印,敏感词语***,登陆状态的判断(会话过滤)。
-
过滤器的执行:
过滤器处理请求采取以下3种行动之一
1)过滤器自身产生一个响应,返回给客户端
2)把请求转发给过滤器链中的下一个过滤器,如果是最后一个过滤器则将请求转发给请求指定资源
3)把请求转发到另一资源 -
简单了解(
瞥一眼就行)不同功能的过滤器
过滤器API
- Filter接口:
1)init()方法:Servlet容器最先调用过滤器的init()方法,初始化过滤器,生命周期中只调用一次
2)doFilter()方法:Servlet容器调用doFilter()方法处理请求
3)destroy()方法:Servlet容器最后调用destroy()方法,释放过滤器所占的资源 - 过滤器相关接口、类:
配置过滤器
- 简单配置过滤器,可以通过注解的方式,或者通过xml文件的配置(见下述代码的栗子)
如下图的Filter就是过滤的/servlet路径下的所有请求;
- 当涉及多个过滤器共同过滤相同的资源时:
Filter过滤器链
1)Web容器加载的顺序
:web.xml文件中配置的Filter的倒序
2)拦截执行顺序
: web.xml文件中配置的Filter的正序
3)销毁顺序:
web.xml文件中配置的Filter的倒序
关于这个顺序,可以参考下面例子中的代码及效果。
举一个简单的例子:
创建一个三个filter,在xml中进行配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>fiter</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>filter1</filter-name>
<filter-class>com.xhx.filter.Filter1</filter-class>
<init-param>
<param-name>name</param-name>
<param-value>tom1</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>filter1</filter-name>
<url-pattern>/servlet/s1</url-pattern>
</filter-mapping>
<filter>
<filter-name>filter2</filter-name>
<filter-class>com.xhx.filter.Filter2</filter-class>
</filter>
<filter-mapping>
<filter-name>filter2</filter-name>
<url-pattern>/servlet/s1</url-pattern>
</filter-mapping>
<filter>
<filter-name>filter3</filter-name>
<filter-class>com.xhx.filter.Filter3</filter-class>
</filter>
<filter-mapping>
<filter-name>filter3</filter-name>
<url-pattern>/servlet/s1</url-pattern>
</filter-mapping>
</web-app>
三个filter:
package com.xhx.filter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet Filter implementation class filter1
*/
//@WebFilter("/servlet/*")
public class Filter1 implements Filter {
public Filter1() {
System.out.println("Filter1的构造方法----");
}
/**
* @see Filter#destroy()
*/
public void destroy() {
System.out.println("Filter1---->" + "destroy");
}
/**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("Filter1---->" + "doFilter");
/*//1.分析请求,过滤器自己创建一个响应返回
PrintWriter writer = response.getWriter();
writer.write("Filter1*****Filter1");
//作用1. 分析请求,将请求(转发/重定向)发送给指定的资源
HttpServletResponse resp = (HttpServletResponse) response;
resp.sendRedirect("/servlet/s2");
HttpServletRequest req = (HttpServletRequest) request;
//req.getRequestDispatcher("").forward(request, response);
//2.对请求进行预处理,设置请求信息,把请求封装成符合规则的对象
//request.setCharacterEncoding("utf-8");
//request.setAttribute("para1", "value1");
//3.在响应到达客户端前处理响应,将响应封装成符合规则的对象。
response.setContentType("text/html;charset=utf-8");
chain.doFilter(request, response);*/
System.out.println("Filter1 ---- doFilter -----Before---"+config.getInitParameter("name"));
chain.doFilter(request, response);
System.out.println("Filter1 ---- doFilter -----After---");
}
/**
* @see Filter#init(FilterConfig)
*/
public FilterConfig config;
String p1;
public void init(FilterConfig fConfig) throws ServletException {
System.out.println("Filter1---->" + "init");
config=fConfig;
p1 = config.getInitParameter("name");
System.out.println(p1);
}
}
package com.xhx.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
/**
* Servlet Filter implementation class Filter2
*/
//@WebFilter("/Filter2")
public class Filter2 implements Filter {
/**
* Default constructor.
*/
public Filter2() {
// TODO Auto-generated constructor stub
}
/**
* @see Filter#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
}
/**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("Filter2 ---- doFilter -----Before---");
chain.doFilter(request, response);
System.out.println("Filter2 ---- doFilter -----After---");
}
/**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
}
}
package com.xhx.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
/**
* Servlet Filter implementation class Filter3
*/
//@WebFilter("/Filter3")
public class Filter3 implements Filter {
/**
* Default constructor.
*/
public Filter3() {
// TODO Auto-generated constructor stub
}
/**
* @see Filter#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
}
/**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("Filter3 ---- doFilter -----Before---");
chain.doFilter(request, response);
System.out.println("Filter3 ---- doFilter -----After---");
}
/**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
}
}
以及一个servlet
package com.xhx.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Servlet1
*/
@WebServlet("/servlet/s1")
public class Servlet1 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Servlet1() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String name = request.getParameter("uname");
System.out.println("xxx");
PrintWriter writer = response.getWriter();
writer.write(name+"<br>");
writer.write("firsyt");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
其他的不重要,打印出来的内容如下:
从这个例子中,可以感受到,filter的执行顺序。
一个练习:
Filter注意事项:
-
Filter配置过滤器链
-
Servlet容器可以对同一个过滤器对象运行读个线程来同时处理多个请求。
-
上例代码中用了一种模糊区配的方法。一共有三种简单而言:
1)/servlet/s1
精确区配;
2)/servlet/*
/servlet路径下的所有请求;
3)*.do
以do结尾的请求。