【问题标题】:How to throw an custom Exception by an resource to error.jsp from filter如何从过滤器向error.jsp中的资源抛出自定义异常
【发布时间】:2014-01-25 00:48:28
【问题描述】:

当我们从过滤器转发到资源时,我们使用 RequestDispatcher 或 FilterChain > doFilter 方法。我在我的代码中使用了 FilterChain doFilter,因为它转发到资源(如果在描述符中找不到过滤器),否则过滤。

我将这个 FilterChain doFilter 调用包装在 try catch 块中,并且过滤器只抛出 ServletException 或 IOException,我能否从过滤器中抛出任何自定义异常(由我的操作类抛出),而不是抛出 ServletException。我做错了什么吗?请指教……

【问题讨论】:

    标签: exception filter


    【解决方案1】:

    试试这个:

    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 是一个自定义的异常。这可能对您检查有帮助。

    【讨论】:

    • 我已经有这种自定义异常了,但是我无法从过滤器中抛出这个异常,因为过滤器只抛出了servlet异常......
    • 我为你的场景改变了我的答案,检查一下它可能对你有帮助
    • chain.doFilter 转发到资源或过滤器,如果转发到资源,如果资源抛出任何异常都会抛出异常......对吗???
    • 是的。否则,您可以捕获该异常并相应地执行一些操作catch (WIUExceptione) {httpResponse.sendRedirect(contextPath+"/jsp/LoginUser.cactus");}
    猜你喜欢
    • 2017-04-20
    • 2017-10-17
    • 2021-03-12
    • 1970-01-01
    • 2021-08-09
    • 2021-11-05
    • 2018-06-04
    • 2018-08-08
    • 1970-01-01
    相关资源
    最近更新 更多