Java Servlet Filter 詳解

Servlet Filter 可以攔截所有指向服務(wù)端的請求。

Servlet Filter.png

如果你想創(chuàng)建一個ServletFilter ,你需要實現(xiàn)一個接口javax.servlet.Filter

import javax.servlet.*;
import java.io.IOException;

public class SimpleServletFilter implements Filter {

    public void init(FilterConfig filterConfig) throws ServletException {
    }

    public void doFilter(ServletRequest request, ServletResponse response,
                                    FilterChain filterChain)
    throws IOException, ServletException {

    }

    public void destroy() {
    }
}

servlet filter 一旦被裝載,首先會調(diào)用它的init()方法。

當(dāng)HTTP請求指向過濾器截獲的服務(wù)端,過濾器可以檢查URI,請求參數(shù)和請求頭,并根據(jù)它決定是否要將請求阻止或轉(zhuǎn)發(fā)到目標(biāo)Servlet,JSP 等

具有攔截功能的方法是doFilter()

public void doFilter(ServletRequest request, ServletResponse response,
                     FilterChain filterChain)
throws IOException, ServletException {

    String myParam = request.getParameter("myParam");

    if(!"blockTheRequest".equals(myParam)){
        filterChain.doFilter(request, response);
    }
}

Notice how the doFilter() method checks a request parameter, myParam, to see if it equals the string "blockTheRequest". If not, the request is forwarded to the target of the request, by calling the filterChain.doFilter() method. If this method is not called, the request is not forwarded, but just blocked.

The servlet filter above just ignores the request if the request parameter myParam equals "blockTheRequest". You can also write a different response back to the browser. Just use the ServletResponse object to do so, just like you would inside a servlet.

You may have to cast the ServletResponse to a HttpResponse to obtain a PrintWriter from it. Otherwise you only have the OutputStream available via getOutputStream().

Here is an example:
這個doFilter()方法檢查request參數(shù):myParam,看它是不是和"blockTheRequest"相愛南瓜燈,如果不是,這個請求會被filterChain.doFilter()方法調(diào)用,如果沒有被調(diào)用,線程掛起。如果相等,你能在ServletResponse對象中寫一寫返回給瀏覽器的數(shù)據(jù)。

必須將ServletResponse強制轉(zhuǎn)換為HttpResponse才可以從中獲取PrintWriter。 否則,只能通過getOutputStream()獲得OutputStream。

public void doFilter(ServletRequest request, ServletResponse response,
                     FilterChain filterChain)
throws IOException, ServletException {

    String myParam = request.getParameter("myParam");

    if(!"blockTheRequest".equals(myParam)){
        filterChain.doFilter(request, response);
        return;
    }

    HttpResponse httpResponse = (HttpResponse) httpResponse;
    httpResponse.getWriter().write("a different response... e.g in HTML");
}

在web.xml里配置過濾器/攔截器

<filter>
    <filter-name>myFilter</filter-name>
    <filter-class>servlets.SimpleServletFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>myFilter</filter-name>
    <url-pattern>*.simple</url-pattern>
</filter-mapping>

通過這種配置,所有URL以.simple結(jié)尾的請求都將被servlet過濾器攔截。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,680評論 19 139
  • 本文包括:1、Filter簡介2、Filter是如何實現(xiàn)攔截的?3、Filter開發(fā)入門4、Filter的生命周期...
    廖少少閱讀 7,526評論 3 56
  • 監(jiān)聽器(listener) 監(jiān)聽器簡介 :監(jiān)聽器就是一個實現(xiàn)特定接口的普通java程序,這個程序?qū)iT用于監(jiān)聽另一個...
    奮斗的老王閱讀 2,688評論 0 53
  • 這部分主要是與Java Web和Web Service相關(guān)的面試題。 96、闡述Servlet和CGI的區(qū)別? 答...
    雜貨鋪老板閱讀 1,504評論 0 10
  • 重說三: 這不是軟文,我沒收錢。但希望廠商看到這篇文章之后能主動給我打錢。 這不是軟文,我沒收錢。但希望廠商看到這...
    安慶盧十四閱讀 708評論 0 2

友情鏈接更多精彩內(nèi)容