在JSP中打印堆栈跟踪

时间:2013-11-28 02:47:52

标签: java jsp

好吧,我对这个JSP事物有点新意,所以,如果这不是正确的做法,请不要把我钉在十字架上。

我需要在error.jsp页面中打印stackTrace,不仅是因为它接收的异常,而且还包括所有原因链。所以,我首先把它放在我的web.xml中:

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

然后,我写了这个error.jsp文件:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isErrorPage="true" %>
<%@ page import="java.lang.Throwable" %>
<%@ page import="java.lang.StackTraceElement" %>

<html>
    <head>
        <title>An Error!</title>
    </head>

    <body>
            <h1>An Error!</h1>
            <table width="100%" border="1">
                <tr valign="top">
                    <td width="40%"><b>Error:</b></td>
                    <td>${pageContext.exception}</td>
                </tr>
                <tr valign="top">
                    <td><b>URI:</b></td>
                    <td>${pageContext.errorData.requestURI}</td>
                </tr>
                <tr valign="top">
                    <td><b>Status code:</b></td>
                    <td>${pageContext.errorData.statusCode}</td>
                </tr>
                <tr valign="top">
                    <td><b>Stack trace:</b></td>
                    <td>
                        <c:forEach var="trace" 
                         items="${pageContext.exception.stackTrace}">
                            <p>${trace}</p>
                        </c:forEach>
                    </td>
                </tr>

            <% 
                Throwable cause =  pageContext.getException().getCause();
                while(cause != null) {
            %>

                <tr valign="top">
                    <td><b>Caused by:</b></td>
                    <td>
                        <% out.println(cause); %>
                    </td>
                </tr>

                <tr valign="top">
                    <td><b>Stack trace:</b></td>
                    <td>
                        <%
                        StackTraceElement[] stackTrace = cause.getStackTrace();
                        for (int i = 0; i < stackTrace.length; i++) {
                            %> <p> <%
                            out.println(stackTrace[i]);
                            %> </p> <%
                        }
                        %>
                    </td>
                </tr>

            <%  cause = cause.getCause(); //Get next cause for the while loop
            }

            if (cause == null){
                %>
                    <tr valign="top">
                        <td><b>No further Cause</b></td>
                    </tr>
                <%         
            }

        %>

        </table>
    </body>
</html>

它正常工作并且做得很好,但我认识到这是一个丑陋的黑客。有没有办法更优雅地做到这一点?

谢谢

编辑:

忘记提及部分代码的来源:http://www.tutorialspoint.com/jsp/jsp_exception_handling.htm

1 个答案:

答案 0 :(得分:0)

查看标题为“为错误页面使用JSTL标记”的教程部分。 JSTL标记是编写更清晰的JSP的几种方法之一。正如您所注意到的,JSP中任何scriptlet(即Java)代码的存在都是丑陋的。 JSTL提供了清晰的语法,用于引用各种JSP作用域(页面,请求,会话,应用程序......)中的数据,而无需编写Java代码。如果您计划进行JSP开发,我建议您花时间熟悉这个主题。这是值得的。

相关问题