代码之家  ›  专栏  ›  技术社区  ›  andPat

web.xml中的<error page>标记没有捕获java.lang.Throwable异常

  •  3
  • andPat  · 技术社区  · 13 年前

    我有一个用servlet&JSP中。我将我的应用程序配置为抛出 IllegalArgumentException 如果我插入错误的参数。 然后我以这种方式配置了我的web.xml文件:

    <error-page>
        <error-code>404</error-code>
        <location>/error.jsp</location>
    </error-page>
    <error-page>
        <exception-type>java.lang.Throwable</exception-type>
        <location>/error.jsp</location>
    </error-page>
    

    当我站起来的时候 404 error ,然后它工作并调用 error.jsp ,但当我站起来时 java.lang.IllegalArgumentException ,那么它就不起作用了,我有 blank page 而不是 错误.jsp 为什么?

    服务器是Glassfish,日志显示IllegalArgumentException上升了。

    3 回复  |  直到 5 年前
        1
  •  7
  •   BalusC    5 年前

    你不应该抓住并压制它,而应该让它过去。

    即不要这样做:

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            doSomethingWhichMayThrowException();
        } catch (IllegalArgumentException e) {
            e.printStackTrace(); // Or something else which totally suppresses the exception.
        }
    }
    

    但还是顺其自然吧:

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doSomethingWhichMayThrowException();
    }
    

    或者,如果你真的打算捕获它进行日志记录(我宁愿使用过滤器,但ala),那么重新抛出它:

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            doSomethingWhichMayThrowException();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            throw e;
        }
    }
    

    或者,如果它不是运行时异常,则在 ServletException ,它将被容器自动展开:

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            doSomethingWhichMayThrowException();
        } catch (NotARuntimeException e) {
            throw new ServletException(e);
        }
    }
    

    另请参阅:

        2
  •  1
  •   oᴉɹǝɥɔ    5 年前

    另一种(简化的)方法是不为各种 <error-code> <exception-type> 情况,但有一种,有点包罗万象,比如。

    <error-page>
        <location>/error-page.jsp</location>
    </error-page>
    

    在你的内心深处 error-page.jsp 您可以确定原因,无论是返回状态代码还是异常,如下所述: https://www.tutorialspoint.com/servlets/servlets-exception-handling.htm 这些常数是标准的一部分 Servlet 3.0 API .

    例如一个基元 错误页面.jsp 放置在web应用程序根目录中的响应处理程序可能如下所示:

    Server encountered a situation
    Status code: <%=(Integer) request.getAttribute(javax.servlet.RequestDispatcher.ERROR_STATUS_CODE)%>
    <br>
    Exception: <%=(Throwable) request.getAttribute(javax.servlet.RequestDispatcher.ERROR_EXCEPTION)%>
    

    出于安全原因,我不建议向客户端发送确切的异常类型;这只是一个如何处理内部不同类型错误和响应状态的示例 JSP 处理者;可以使用servlet代替JSP。

    一个常见的catch-all处理程序和一个每个状态代码当然取决于情况和需求。

        3
  •  0
  •   Community Mohan Dere    5 年前

    我今天也有同样的问题。(JavaEE 7和Glassfish 4.0)

    问题似乎是框架将其检查为String,而不是使用Class。

    基于字符串的检查(假设)

    当异常被twrown时, e.getClass() 与进行比较 <exception-type> 作为字符串。 所以你不能使用继承。

    请注意,嵌套类必须指向“$”,而不是“。”(与getClass()方法相同)。

    基于类的检查

    框架创建类的实例,并且 <异常类型> 文本引用它,并且 class.isInstance() 用于检查。

    这将需要反思,而策略文件可能会破坏它。

    我希望这一回应能解决未来的问题。