簡體   English   中英

如何使用過濾器處理Servlet中引發的異常

[英]How to use a filter to handle exception that are throws in the servlet

我的目的是創建一個過濾器來處理servlet中引發的異常。

假設我有這個過濾器:

public class FiltroAccess implements Filter{

    public void destroy() {
        // TODO Auto-generated method stub

    }

    public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
            throws IOException, ServletException {
        //handle exception

    }

    public void init(FilterConfig arg0) throws ServletException {
        // TODO Auto-generated method stub

    }
}

然后在我的servlet拋出Exception

我該如何處理過濾器中的Exeception?

有人可以幫助我嗎?

只需將過濾器鏈調用包含在try / catch塊中:

public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
        throws IOException, ServletException {
    try {
        arg2.doFilter(arg0, arg1);
    }
    catch (Exception e) {
        // Handle exception here
    }
}

唯一的問題是,ServletResponse已經提交(發送給客戶端),您將無法更改標頭。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM