Exception 如何通过资源将自定义异常从筛选器抛出到error.jsp

Exception 如何通过资源将自定义异常从筛选器抛出到error.jsp,exception,filter,Exception,Filter,当我们从筛选器转发资源时,我们使用的是RequestDispatcher或FilterChain>doFilter方法。我在代码中使用了FilterChainDoFilter,因为它会转发到资源(若描述符中找不到筛选器),否则会转发到筛选器 我在try-catch块中包装了这个FilterChain-doFilter调用,并且filter只抛出ServletException或IOException,我可以从filter中抛出任何自定义异常(由我的操作类抛出),而不是抛出ServletExcep

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

我在try-catch块中包装了这个FilterChain-doFilter调用,并且filter只抛出ServletException或IOException,我可以从filter中抛出任何自定义异常(由我的操作类抛出),而不是抛出ServletException。我做错什么了吗。请告诉我……

试试这个:

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异常……我更改了针对您的场景的答案,选中此选项可能有助于youchain.doFilter转发到资源或筛选器(如果转发到资源),并且如果资源引发任何异常,它将引发异常。。。对吧???对。否则,您可以捕获该异常并相应地执行一些操作。
catch(wiueexception){httpResponse.sendrirect(contextPath+“/jsp/logiuser.cactus”);}