JSP 定制错误页 - 异常变量为空



我正在为在tomcat 7上运行的JSP应用程序创建一个简单的自定义错误页面。我在打印异常时遇到问题。打印时的异常变量:

<%= exception %> 

始终为空。

网络.xml

<error-page>
  <error-code>404</error-code>
   <location>/error.jsp</location>
</error-page>
<error-page>
  <error-code>500</error-code>
  <location>/error.jsp</location>
</error-page>
<error-page>
  <exception-type>java.lang.Exception</exception-type>
  <location>/error.jsp</location>
</error-page>

错误.jsp

<%@ page isErrorPage="true" import="java.io.*" %>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
  <title>Error Page</title>
  </head>
  <body>
     <div>
       <h3>Error</h3> 
       <h4><b>Exception:</b><br></h4> 
       <p><i><%= exception %></i><p>
     </div>
     <footer></footer>
  </body>

命令

我还补充说:

<%@ page errorPage="errorPage.jsp" %>

到我的所有页面。

错误

当它运行时,它只会为异常变量打印 null。如果我尝试将其
更改为 exception.printStackTrace(response.getWriter()

我会收到如下所示的错误:

SEVERE: Exception Processing ErrorPage[errorCode=404, location=/error.jsp]
org.apache.jasper.JasperException: /error.jsp (line: 1, column: 32) equal symbol expected

改用表达式语言:

<%@ page isErrorPage="true" %>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
  <title>Error Page</title>
  </head>
  <body>
     <div>
       <h3>Error</h3> 
       <h4><b>Exception:</b><br></h4> 
       <p><i>${pageContext.exception.message}</i><p>
     </div>
     <footer></footer>
  </body>
</html>

请注意,仅当异常来自生成 JSP 时的Exception时,这将打印异常,例如,具有抛出未处理Exception的脚本代码。如果它来自 400 或 500 HTTP 响应代码,则根本不会打印任何文本。

最新更新