将异常从servlet转发到jsp页面的好方法是什么?

NSF*_*NSF 4 jsp tomcat servlets exception

我知道我可以像这样在web.xml中添加一些内容

<error-page>  
   <exception-type>java.lang.Throwable</exception-type>  
   <location>/error.jsp</location>  
</error-page>
Run Code Online (Sandbox Code Playgroud)

但是jsp页面不会显示任何构造性信息,因为它不会得到究竟是什么异常.我知道我们可以通过各种方式将不同的异常转发到不同的页面,exception-type但是在web.xml中写入太多了.我希望一个页面足够,另一个页面用于处理像404这样的错误.

那么我应该如何将异常信息传递给jsp页面呢?使用会话?

理想情况可能是页面获取异常信息并显示有关它的一些相关消息,而不向用户显示异常.相反,它可以将其记录到文件中以供将来参考.实现这一目标的最佳方法是什么?谢谢.

Bal*_*usC 10

关于异常的信息已经由几个请求属性可用.你可以找到所有这些属性的名称RequestDispatcher的javadoc:

因此,简而言之,此JSP示例应显示所有可能的异常详细信息:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
<ul>
    <li>Exception: <c:out value="${requestScope['javax.servlet.error.exception']}" /></li>
    <li>Exception type: <c:out value="${requestScope['javax.servlet.error.exception_type']}" /></li>
    <li>Exception message: <c:out value="${requestScope['javax.servlet.error.message']}" /></li>
    <li>Request URI: <c:out value="${requestScope['javax.servlet.error.request_uri']}" /></li>
    <li>Servlet name: <c:out value="${requestScope['javax.servlet.error.servlet_name']}" /></li>
    <li>Status code: <c:out value="${requestScope['javax.servlet.error.status_code']}" /></li>
</ul>
Run Code Online (Sandbox Code Playgroud)

此外,您还可以显示以下有用信息:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<jsp:useBean id="date" class="java.util.Date" />
...
<ul>
    <li>Timestamp: <fmt:formatDate value="${date}" type="both" dateStyle="long" timeStyle="long" /></li>
    <li>User agent: <c:out value="${header['user-agent']}" /></li>
</ul>
Run Code Online (Sandbox Code Playgroud)

ExceptionJSP中的具体实例本身仅在${exception}将页面标记为错误页面时可用:

<%@ page isErrorPage="true" %>
...
${exception}
Run Code Online (Sandbox Code Playgroud)

只有当您使用的是EL 2.2或更新版本时,才可以打印其堆栈跟踪,如下所示:

<%@ page isErrorPage="true" %>
...
<pre>${pageContext.out.flush()}${exception.printStackTrace(pageContext.response.writer)}</pre>
Run Code Online (Sandbox Code Playgroud)

或者,如果您还没有使用EL 2.2,那么为此创建一个自定义EL函数:

public final class Functions {

    private Functions() {}

    public static String printStackTrace(Throwable exception) {
        StringWriter stringWriter = new StringWriter();
        exception.printStackTrace(new PrintWriter(stringWriter, true));
        return stringWriter.toString();
    }

}
Run Code Online (Sandbox Code Playgroud)

注册地址/WEB-INF/functions.tld:

<?xml version="1.0" encoding="UTF-8" ?>
<taglib 
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
    version="2.1">

    <display-name>Custom Functions</display-name>    
    <tlib-version>1.0</tlib-version>
    <uri>http://example.com/functions</uri>

    <function>
        <name>printStackTrace</name>
        <function-class>com.example.Functions</function-class>
        <function-signature>java.lang.String printStackTrace(java.lang.Throwable)</function-signature>
    </function>
</taglib>
Run Code Online (Sandbox Code Playgroud)

并且可以用作

<%@ taglib prefix="my" uri="http://example.com/functions" %>
...
<pre>${my:printStackTrace(exception)}</pre>
Run Code Online (Sandbox Code Playgroud)

至于异常的记录,最简单的地方是一个映射到URL模式的过滤器,/*基本上做如下:

try {
    chain.doFilter(request, response);
} catch (ServletException e) {
    log(e.getRootCause());
    throw e;
} catch (IOException e) { // If necessary? Usually not thrown by business code.
    log(e);
    throw e;
}
Run Code Online (Sandbox Code Playgroud)