2012-06-18 43 views
20

在我的應用程序中,我想應用一些過濾器,但我不希望所有請求都必須轉到該過濾器。這將是一個性能問題,因爲我們已經有一些其他的過濾器。如何僅將servlet過濾器應用於http post方法

我想我的過濾器只適用於HTTP POST方法..有什麼辦法嗎?

請幫我找到一個方法。

感謝 基蘭

回答

24

當局沒有現成此可用功能。 A Filter在適用於所有HTTP方法上沒有開銷。但是,如果你在Filter代碼中有一些邏輯開銷,那麼你不應該將該邏輯應用於不需要的HTTP方法。

下面是示例代碼:

public class HttpMethodFilter implements Filter 
{ 
    public void init(FilterConfig filterConfig) throws ServletException 
    { 

    } 

    public void doFilter(ServletRequest request, ServletResponse response, 
     FilterChain filterChain) throws IOException, ServletException 
    { 
     HttpServletRequest httpRequest = (HttpServletRequest) request;   
     if(httpRequest.getMethod().equalsIgnoreCase("POST")){ 

     } 
     filterChain.doFilter(request, response); 
    } 

    public void destroy() 
    { 

    } 
} 
+0

嗨,你可以知道如何在JSON請求(後)我做這件事是索取,而如何做到這一點的職位申請春季逃避者的SQL和HTML?看看http://stackoverflow.com/questions/1812891/java-escape-string-to-prevent-sql-injection/39440337#39440337 – shareef

相關問題