一、過(guò)濾器的基本概念
Java中的Filter 并不是一個(gè)標(biāo)準(zhǔn)的Servlet ,它不能處理用戶(hù)請(qǐng)求,也不能對(duì)客戶(hù)端生成響應(yīng)。 主要用于對(duì)HttpServletRequest 進(jìn)行預(yù)處理,也可以對(duì)HttpServletResponse 進(jìn)行后處理,是個(gè)典型的處理鏈。過(guò)濾鏈的好處是,執(zhí)行過(guò)程中任何時(shí)候都可以打斷,只要不執(zhí)行chain.doFilter()就不會(huì)再執(zhí)行后面的過(guò)濾器和請(qǐng)求的內(nèi)容。而在實(shí)際使用時(shí),就要特別注意過(guò)濾鏈的執(zhí)行順序問(wèn)題.。
二、過(guò)濾器的運(yùn)行原理
過(guò)濾器(Filter)接口中有一個(gè)doFilter方法,當(dāng)開(kāi)發(fā)人員編寫(xiě)好Filter,并配置對(duì)哪個(gè)web資源進(jìn)行攔截后,WEB服務(wù)器每次在調(diào)用web資源的service方法之前,都會(huì)先調(diào)用一下filter的doFilter方法.web服務(wù)器在調(diào)用doFilter方法時(shí),會(huì)傳遞request,reponse,filterChain對(duì)象進(jìn)來(lái),filterChain對(duì)象是filter接口中最重要的一個(gè)對(duì)象,它也提供了一個(gè)doFilter方法,開(kāi)發(fā)人員可以根據(jù)需求決定是否調(diào)用此方法,調(diào)用該方法,則web服務(wù)器就會(huì)調(diào)用web資源的service方法,即web資源就會(huì)被訪問(wèn),而且我們能夠在調(diào)用doFilter方法之前先對(duì)request、response進(jìn)行預(yù)處理,否則web資源不會(huì)被訪問(wèn)。
原理圖:
三、如何自定義過(guò)濾器
自定義過(guò)濾器的步驟:
1. 編寫(xiě)java類(lèi)實(shí)現(xiàn)Filter接口,并實(shí)現(xiàn)其doFilter方法。
2. 在 web.xml 文件中使用<filter>和<filter-mapping>元素對(duì)編寫(xiě)的filter類(lèi)進(jìn)行注冊(cè),并設(shè)置它所能攔截的資源。
Java代碼:
public class FilterDemo01 implements Filter {
public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
System.out.println("進(jìn)入FilterDemo01過(guò)濾器");
}
@Override
public void destroy() {
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {}
}
Web.xml配置文件
<filter>
<!-- 為Filter取一個(gè)唯一的名字 -->
<filter-name>FilterDemo01</filter-name>
<!-- Filter的全路徑類(lèi)名,必須提供無(wú)參構(gòu)造器 -->
<filter-class>cn.itcast.javaee.filter.base.FilterDemo01</filter-class>
</filter>
<filter-mapping>
<filter-name>FilterDemo01</filter-name>
<!-- Filter能過(guò)濾的URL路徑 -->
<url-pattern>/DynaServlet</url-pattern>
</filter-mapping>
四、過(guò)濾器詳解
4.1過(guò)濾器的生命周期
1. init(FilterConfig):在服務(wù)器啟動(dòng)時(shí)會(huì)創(chuàng)建Filter實(shí)例,并且每個(gè)類(lèi)型的Filter只創(chuàng)建一個(gè)實(shí)例,從此不再創(chuàng)建!在創(chuàng)建完Filter實(shí)例后,會(huì)馬上調(diào)用init()方法完成初始化工作,這個(gè)方法只會(huì)被執(zhí)行一次;
2. doFilter(ServletRequest req,ServletResponse res,FilterChain chain):這個(gè)方法會(huì)在用戶(hù)每次訪問(wèn)“目標(biāo)資源(<url->pattern>index.jsp</url-pattern>)”時(shí)執(zhí)行,如果需要“放行”,那么需要調(diào)用FilterChain的doFilter(ServletRequest,ServletResponse)方法,如果不調(diào)用FilterChain的doFilter()方法,那么目標(biāo)資源將無(wú)法執(zhí)行;
3. destroy():服務(wù)器會(huì)在創(chuàng)建Filter對(duì)象之后,把Filter放到緩存中一直使用,通常不會(huì)銷(xiāo)毀它。一般會(huì)在服務(wù)器關(guān)閉時(shí)銷(xiāo)毀Filter對(duì)象,在銷(xiāo)毀Filter對(duì)象之前,服務(wù)器會(huì)調(diào)用Filter對(duì)象的destory()方法。
4.2 FilterConfig對(duì)象
Filter接口中的init()方法的參數(shù)類(lèi)型為FilterConfig類(lèi)型。它的功能與ServletConfig相似,與web.xml文件中的配置信息對(duì)應(yīng)。下面是FilterConfig的功能介紹:
l ServletContext getServletContext():獲取ServletContext的方法;
l String getFilterName():獲取Filter的配置名稱(chēng);與<filter-name>元素對(duì)應(yīng);
l String getInitParameter(String name):獲取Filter的初始化配置,與<init-param>元素對(duì)應(yīng);
l Enumeration getInitParameterNames():獲取所有初始化參數(shù)的名稱(chēng)。
4.3 FilterChain過(guò)濾器
doFilter()方法的參數(shù)中有一個(gè)類(lèi)型為FilterChain的參數(shù),它只有一個(gè)方法:doFilter(ServletRequest,ServletResponse)。前面我們說(shuō)doFilter()方法的放行,讓請(qǐng)求流訪問(wèn)目標(biāo)資源!但這么說(shuō)不嚴(yán)密,其實(shí)調(diào)用該方法的意思是,“我(當(dāng)前Filter)”放行了,但不代表其他人(其他過(guò)濾器)也放行。因?yàn)橐粋€(gè)目標(biāo)資源上,可能部署了多個(gè)過(guò)濾器,就好比孫悟空去取經(jīng)要經(jīng)過(guò)九九八十一難一樣,消滅了第一個(gè)妖怪還不是成功。然后我們過(guò)濾器也是一樣,如果當(dāng)前過(guò)濾器是最后一個(gè)過(guò)濾器,那么調(diào)用chain.doFilter()方法表示執(zhí)行目標(biāo)資源,而不是最后一個(gè)過(guò)濾器,那么chain.doFilter()表示執(zhí)行下一個(gè)過(guò)濾器的doFilter()方法。
代碼:
<filter>
<filter-name>filter1</filter-name>
<filter-class>cn.itcast.filter.MyFilter1</filter-class>
</filter>
<filter-mapping>
<filter-name>filter1</filter-name>
<url-pattern>/index.jsp</url-pattern>
</filter-mapping>
<filter>
<filter-name>filter2</filter-name>
<filter-class>cn.itcast.filter.MyFilter2</filter-class>
</filter>
<filter-mapping>
<filter-name>myFilter2</filter-name>
<url-pattern>/index.jsp</url-pattern>
</filter-mapping> |
public class MyFilter1 extends HttpFilter {
public void doFilter(HttpServletRequest request, HttpServletResponse response,
FilterChain chain) throws IOException, ServletException {
System.out.println("filter1 start...");
chain.doFilter(request, response);//放行,執(zhí)行MyFilter2的doFilter()方法
System.out.println("filter1 end...");
}
} |
public class MyFilter2 extends HttpFilter {
public void doFilter(HttpServletRequest request, HttpServletResponse response,
FilterChain chain) throws IOException, ServletException {
System.out.println("filter2 start...");
chain.doFilter(request, response);//放行,執(zhí)行目標(biāo)資源
System.out.println("filter2 end...");
}
} |
<body>
index.jsp
</body> |
結(jié)果:
filter1 start...
filter2 start...
index.jsp
filter2 end...
filter1 end...
4.3過(guò)濾器的四種攔截方式
我們來(lái)做個(gè)測(cè)試,寫(xiě)一個(gè)過(guò)濾器,指定過(guò)濾的資源為b.jsp,然后我們?cè)跒g覽器中直接訪問(wèn)b.jsp,你會(huì)發(fā)現(xiàn)過(guò)濾器執(zhí)行了!但是,當(dāng)我們?cè)赼.jsp中request.getRequestDispathcer(“/b.jsp”).forward(request,response)時(shí),就不會(huì)再執(zhí)行過(guò)濾器了!也就是說(shuō),默認(rèn)情況下,只能直接訪問(wèn)目標(biāo)資源才會(huì)執(zhí)行過(guò)濾器,而forward執(zhí)行目標(biāo)資源,不會(huì)執(zhí)行過(guò)濾器!
其實(shí)過(guò)濾器有四種攔截方式!分別是:REQUEST、FORWARD、INCLUDE、ERROR。
l REQUEST:直接訪問(wèn)目標(biāo)資源時(shí)執(zhí)行過(guò)濾器。包括:在地址欄中直接訪問(wèn)、表單提交、超鏈接、重定向,只要在地址欄中可以看到目標(biāo)資源的路徑,就是REQUEST;
l FORWARD:轉(zhuǎn)發(fā)訪問(wèn)執(zhí)行過(guò)濾器。包括RequestDispatcher#forward()方法、<jsp:forward>標(biāo)簽都是轉(zhuǎn)發(fā)訪問(wèn);
l INCLUDE:包含訪問(wèn)執(zhí)行過(guò)濾器。包括RequestDispatcher#include()方法、<jsp:include>標(biāo)簽都是包含訪問(wèn);
l ERROR:當(dāng)目標(biāo)資源在web.xml中配置為<error-page>中時(shí),并且真的出現(xiàn)了異常,轉(zhuǎn)發(fā)到目標(biāo)資源時(shí),會(huì)執(zhí)行過(guò)濾器。
可以在<filter-mapping>中添加0~n個(gè)<dispatcher>子元素,來(lái)說(shuō)明當(dāng)前訪問(wèn)的攔截方式。
<filter-mapping>
<filter-name>myfilter</filter-name>
<url-pattern>/b.jsp</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping> |
<filter-mapping>
<filter-name>myfilter</filter-name>
<url-pattern>/b.jsp</url-pattern>
</filter-mapping> |
<filter-mapping>
<filter-name>myfilter</filter-name>
<url-pattern>/b.jsp</url-pattern>
<dispatcher>FORWARD</dispatcher>
</filter-mapping> |
其實(shí)最為常用的就是REQUEST和FORWARD兩種攔截方式,而INCLUDE和ERROR都比較少用!ERROR方式如下:
<filter-mapping>
<filter-name>myfilter</filter-name>
<url-pattern>/b.jsp</url-pattern>
<dispatcher>ERROR</dispatcher>
</filter-mapping>
<error-page>
<error-code>500</error-code>
<location>/b.jsp</location>
</error-page> |
<body>
<h1>a.jsp</h1>
<%
if(true)
throw new RuntimeException("嘻嘻~");
%>
</body> |
五、過(guò)濾器的應(yīng)用
5.1 獲取參數(shù)解決全局亂碼
public class EncodingRequest extends HttpServletRequestWrapper {
private String charset;
public EncodingRequest(HttpServletRequest request, String charset) {
super(request);
this.charset = charset;
}
public String getParameter(String name) {
HttpServletRequest request = (HttpServletRequest) getRequest();
String method = request.getMethod();
if(method.equalsIgnoreCase("post")) {
try {
request.setCharacterEncoding(charset);
} catch (UnsupportedEncodingException e) {}
} else if(method.equalsIgnoreCase("get")) {
String value = request.getParameter(name);
try {
value = new String(name.getBytes("ISO-8859-1"), charset);
} catch (UnsupportedEncodingException e) {
}
return value;
}
return request.getParameter(name);
}
} |
EncodingFilter
public class EncodingFilter extends HttpFilter {
public void doFilter(HttpServletRequest request,
HttpServletResponse response, FilterChain chain)
throws IOException, ServletException {
String charset = this.getInitParameter("charset");
if(charset == null || charset.isEmpty()) {
charset = "UTF-8";
}
response.setCharacterEncoding(charset);
response.setContentType("text/html;charset=" + charset);
EncodingRequest res = new EncodingRequest(request, charset);
chain.doFilter(res, response);
}
} |
5.2 自動(dòng)登錄
public class AutoLoginFilter implements Filter {
public void destroy() {
}
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest req, ServletResponse res,FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
//讀取瀏覽器中的cookie
Cookie[] cookies = request.getCookies();
//如果有cookie
if(cookies!=null && cookies.length>0){
Cookie usernameCookie = null;
//迭代
for(Cookie c : cookies){
//詢(xún)找指定的cookie
if("usernameCookie".equals(c.getName())){
//記錄已找到的cookie
usernameCookie = c;
//退出
break;
}
}
//如果找到了指定的cookie
if(usernameCookie!=null){
//獲取該cookie的值,但此時(shí)的值是經(jīng)過(guò)編碼后的
String username = usernameCookie.getValue();
//解碼
username = URLDecoder.decode(username,"UTF-8");
//將用戶(hù)名綁定HttpSession域?qū)ο笾?br />
request.getSession().setAttribute("username",username);
//放行請(qǐng)求
chain.doFilter(request,response);
//如果沒(méi)找到了指定的cookie
}else{
//放行請(qǐng)求
chain.doFilter(request,response);
}
//如果沒(méi)cookie
}else{
//放行請(qǐng)求
chain.doFilter(request,response);
}
}
}
5.3 數(shù)據(jù)壓縮
public class CharGzipFilter implements Filter {
public void destroy() {
}
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest req, ServletResponse res,FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
//創(chuàng)建MyResponse對(duì)象
MyResponse myResponse = new MyResponse(response);
//放行請(qǐng)求,即進(jìn)入charGzip.jsp
chain.doFilter(request,myResponse);
//取出緩存中的數(shù)據(jù)
byte[] data = myResponse.getData();
//顯示
System.out.println("壓縮前:"+data.length);
//進(jìn)行字符壓縮,該類(lèi)只能用于字符流壓縮
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(baos);
gzip.write(data);
gzip.flush();
gzip.close();
//從緩存中取出壓縮后的字節(jié)
data = baos.toByteArray();
//顯示
System.out.println("壓縮后:"+data.length);
//通知瀏覽器需要接收GZIP壓縮格式的數(shù)據(jù)
response.setHeader("content-encoding","gzip");
//將壓強(qiáng)后的數(shù)據(jù)輸出到瀏覽器
response.getOutputStream().write(data);
}
}
/**
* 1)寫(xiě)一個(gè)普通類(lèi)繼承HttpServletResponseWrapper類(lèi)
*/
class MyResponse extends HttpServletResponseWrapper{
private PrintWriter pw;
private ByteArrayOutputStream baos = new ByteArrayOutputStream();
private HttpServletResponse response;
public MyResponse(HttpServletResponse response) {
super(response);
this.response = response;
}
/**
* 4)重寫(xiě)父類(lèi)的getWriter()方法,返回帶有緩存的PrintWriter對(duì)象
*/
@Override
public PrintWriter getWriter() throws IOException {
pw = new PrintWriter(new OutputStreamWriter(baos,"UTF-8"));
return pw;
}
public byte[] getData(){
if(pw!=null){
pw.flush();
}
return baos.toByteArray();
}
}
過(guò)濾器除了以上的應(yīng)用之外,還可以做用戶(hù)權(quán)限的檢查、靜態(tài)資源的緩存優(yōu)化、攔截器的代理、
等應(yīng)用操作,在這里就不一一列舉了。