javax.servlet.error.exception 属性是否总是转换为 java.lang.Exception 类型?
Does javax.servlet.error.exception attribute always cast to java.lang.Exception type?
调度程序类型为 ERROR
的请求中的属性 javax.servlet.error.exception
总是可以转换为 java.lang.Exception
类型?
我看了一下the javadoc,但是没有提到类型必须是java.lang.Exception
OR java.lang.Throwable
.
我问这个问题是因为我正在处理错误页面。如果属性 javax.servlet.error.exception
始终保持 Exception
类型,那么我可以编写如下代码而不必担心所有情况下的 ClassCastException
。
Exception exception = (Exception) request.getAttribute("javax.servlet.error.exception");
我当然可以这样做:
Throwable error = (Throwable)request.getAttribute("javax.servlet.error.exception");
但我的处理方式是 Exception
类型(不是 Throwable
)。
谢谢!
这取决于与 web.xml
中的 <error-page>
条目关联的 <exception-type>
,其 <location>
指的是有问题的基于 JSP/Servlet 的资源。
如果有 none,即您使用的 general error page 没有任何 <exception-type>
,或者您只使用 <error-code>500</error-code>
,或者您没有任何 <error-page>
,然后假设 java.lang.Throwable
.
如果有明确的 <exception-type>
,则假定在 web.xml
中注册的类型完全相同。
换句话说,如果您只想假设 java.lang.Exception
,则根据该类型设置 <exception-type>
。
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/your-error-resource</location>
</error-page>
请注意,当您没有 <error-page>
时,任何 java.lang.Error
(即匹配 java.lang.Throwable
)都会漏掉并最终进入服务器的默认错误页面=26=] 或 java.lang.Throwable
.
另请参阅:
- Error Handler Servlet: how to get exception cause
- How can I print error stack trace in JSP page?
but my target is Exception
您可以使用 ERROR_EXCEPTION_TYPE
找出类型,然后使用 ERROR_EXCEPTION
检索对象
String excType = request.getAttribute(RequestDispatcher.ERROR_EXCEPTION_TYPE);
if ("java.lang.Exception".equals(excType)) {
Exception exception = (Exception) request.getAttribute(RequestDispatcher.ERROR_EXCEPTION);
//....
}
这仍然可能引发 ClassCastException
,但是,如果 ServletRequest.setAttribute()
被用于恶作剧,例如。
如果您还关心其他类型,则可以使用 switch
或反射。
调度程序类型为 ERROR
的请求中的属性 javax.servlet.error.exception
总是可以转换为 java.lang.Exception
类型?
我看了一下the javadoc,但是没有提到类型必须是java.lang.Exception
OR java.lang.Throwable
.
我问这个问题是因为我正在处理错误页面。如果属性 javax.servlet.error.exception
始终保持 Exception
类型,那么我可以编写如下代码而不必担心所有情况下的 ClassCastException
。
Exception exception = (Exception) request.getAttribute("javax.servlet.error.exception");
我当然可以这样做:
Throwable error = (Throwable)request.getAttribute("javax.servlet.error.exception");
但我的处理方式是 Exception
类型(不是 Throwable
)。
谢谢!
这取决于与 web.xml
中的 <error-page>
条目关联的 <exception-type>
,其 <location>
指的是有问题的基于 JSP/Servlet 的资源。
如果有 none,即您使用的 general error page 没有任何 <exception-type>
,或者您只使用 <error-code>500</error-code>
,或者您没有任何 <error-page>
,然后假设 java.lang.Throwable
.
如果有明确的 <exception-type>
,则假定在 web.xml
中注册的类型完全相同。
换句话说,如果您只想假设 java.lang.Exception
,则根据该类型设置 <exception-type>
。
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/your-error-resource</location>
</error-page>
请注意,当您没有 <error-page>
时,任何 java.lang.Error
(即匹配 java.lang.Throwable
)都会漏掉并最终进入服务器的默认错误页面=26=] 或 java.lang.Throwable
.
另请参阅:
- Error Handler Servlet: how to get exception cause
- How can I print error stack trace in JSP page?
but my target is Exception
您可以使用 ERROR_EXCEPTION_TYPE
找出类型,然后使用 ERROR_EXCEPTION
String excType = request.getAttribute(RequestDispatcher.ERROR_EXCEPTION_TYPE);
if ("java.lang.Exception".equals(excType)) {
Exception exception = (Exception) request.getAttribute(RequestDispatcher.ERROR_EXCEPTION);
//....
}
这仍然可能引发 ClassCastException
,但是,如果 ServletRequest.setAttribute()
被用于恶作剧,例如。
如果您还关心其他类型,则可以使用 switch
或反射。