错误页面 Servlet 类



>ı 想要添加一个错误页面我的项目。何时要进行字幕 ı 想要打开错误页面。例如;

    try {
            if ("Plus".equals(op)) {
                v1 = getNumber(value1);
                v2 = getNumber(value2);
                result = String.valueOf(v1 + v2);
            } else if ("Minus".equals(op)){
                v1 = getNumber(value1);
                v2 = getNumber(value2);
                result = String.valueOf(v1 - v2);
            } else{
                result = value1+value2;}
        } catch (NumberFormatException oops) {
             Exception e = new RuntimeException(oops);
             log("*-*-*-*** Bad news - validation failed or has been bypassed*-*-*-*-*", e);
             oops.printStackTrace();
        }
        request.setAttribute("result", result);
        RequestDispatcher dispatcher = request.getRequestDispatcher("RequestDispatcher.jsp");

我在 xml 文件中使用此代码,但不起作用

<error-page>
    <!-- <exception-type>java.lang.NumberFormatException</exception-type> -->
    <exception-type>java.lang.NumberFormatException</exception-type>
    <location>/WEB-INF/BadNumber.jsp</location>
    </error-page>

我的错误消息是:对于输入字符串:"s"

at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)

任何帮助将不胜感激。

您的错误页面不起作用,因为您正在捕获异常而没有再次抛出它。如果要在代码中手动捕获异常,则必须确保它将再次重新引发(使用与您在错误页面中指定的完全相同的类型 <exception-type>

最新更新