java java中的Servlet/过滤器特定异常处理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29914717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:07:29  来源:igfitidea点击:

Servlet/filter specific exception handling in java

javaexceptionservletsexception-handlingservlet-filters

提问by user30622

I have a servlet extending HttpServletand implementing a GETrequest. I also use a filter (from an external library) which is mapped to the above servlet url. Now an exception is thrown by the filter, and as expected I get this

我有一个扩展HttpServlet和实现GET请求的 servlet 。我还使用了映射到上述 servlet url 的过滤器(来自外部库)。现在过滤器抛出了一个异常,正如预期的那样,我得到了这个

SEVERE: Servlet.service() for servlet [myServlet] in context with path [] threw exception

严重:servlet [myServlet] 的 Servlet.service() 在路径 [] 的上下文中引发异常

I know error-pagedescription is probably a standard way to catch this exception, but is there a way to catch the exception from a specific servlet filter ? I already have an error-pagedescription and redirecting to a simple html page. I also don't want to redirect to a jsp page or so, and play around with error parameters. In short, my questions are :

我知道error-page描述可能是捕获此异常的标准方法,但是有没有办法从特定的 servlet 过滤器中捕获异常?我已经有了error-page描述并重定向到一个简单的 html 页面。我也不想重定向到一个jsp页面左右,并玩弄错误参数。简而言之,我的问题是:

  • Is there a simpler, elegant way to catch an exception for a specificservlet and handle them ? The error-pagedescriptor doesn't seem to have any fields to choose the servlet which throws an exception.
  • Is it possible to catch an exception occuring inside a specificfilter and handle them, given that the exception thrown by the filter is not a custom exception ?
  • 有没有更简单、优雅的方法来捕获特定servlet的异常并处理它们?该error-page描述似乎没有任何字段选择哪抛出异常的servlet。
  • 鉴于过滤器抛出的异常不是自定义异常,是否可以捕获特定过滤器内发生的异常并处理它们?

采纳答案by ramp

Can you not extend the Filter and handle the exception thrown by super?

不能扩展Filter,处理super抛出的异常吗?

public class MyFilter extends CustomFilter{

        private static final Map<String, String> exceptionMap = new HashMap<>();

        public void init(FilterConfig config) throws ServletException {
              super.init(config);
              exceptionMap.put("/requestURL", "/redirectURL");
              exceptionMap.put("/someOtherrequestURL", "/someOtherredirectURL");
        }
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
           try{
                   super.doFilter(request, response, chain);
              }catch(Exception e)
                    //log
                    String errURL = exceptionMap.get(request.getRequestURI());
                    if(errURL != null){
                        response.sendRedirect(errURL);
                    }
              }
       }
}