2014-01-07 35 views
0

當我們從過濾器轉發資源時,我們使用的是RequestDispatcher或FilterChain> doFilter方法。我在我的代碼中使用了FilterChain doFilter,因爲它轉發給資源(如果在描述符中找不到過濾器),否則過濾。我可以從過濾器中拋出任何自定義異常(由我的操作類引發),而不是拋出ServletException。我試圖在catch catch塊中包裝此FilterChain doFilter調用,並且僅過濾拋出ServletException或IOException。我做錯了什麼?請告訴我....如何通過資源向過濾器中的error.jsp引發自定義異常

回答

0

試試這個:

public void doFilter(ServletRequest request, 
       ServletResponse response, 
       FilterChain chain) 
       throws java.io.IOException, ServletException,WIUException { 


     String ipAddress = request.getRemoteAddr(); 

     if(ipAddress != null){ 
      throw new WIUException("It is an WIU Exception"); 
     } 
     chain.doFilter(request,response); 
    }

這裏WIUException是一個自定義Exception.This可以幫助你檢查。

+1

我已經有這種自定義異常,但我無法從過濾器拋出此異常,因爲過濾器只拋出servlet異常.... – raky

+0

我改變了我的答案爲您的方案,檢查它可能對您有幫助 –

+0

chain.doFilter轉發到資源或過濾器,如果它轉發到資源,並且它會拋出一個異常,如果資源拋出任何異常......正確??? – raky