3

我有一个使用 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>

当我升起 a404 error时,它可以工作并调用error.jsp,但是当我升起 a 时java.lang.IllegalArgumentException,它不起作用,我有 ablank page而不是error.jsp。为什么?

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

4

3 回答 3

7

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

即不这样做:

@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);
    }
}

也可以看看:

于 2013-04-12T14:27:03.833 回答
1

另一种(简化的)方法不是为各种情况声明多个处理程序,<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的一部分应用程序接口

例如,error-page.jsp放置在 webapp 根目录中的原始响应处理程序可能如下所示:

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。

一个通用的包罗万象的处理程序与每个状态代码一个的处理程序当然取决于情况和要求。

于 2021-03-11T23:05:51.107 回答
0

I have today the same issue. (JavaEE 7 and Glassfish 4.0)

The problem seems that the framework check it as String instead with the Class.

String based check (the hypothesis)

When a Exception is twrown, e.getClass() is compared with <exception-type> as string. So you can't use inheritance.

Note that nested classes must be pointed as '$' instead '.' (same as getClass() method).

Class based check

The framework create an instance of the class, and <exception-type> text refer to it, and the class.isInstance() is used to check.

This will need reflection and policy file could break it.

I hope that this response solves future issues.

于 2014-06-11T21:19:01.247 回答