【问题标题】:JSP custom error page - Exception variable is nullJSP 自定义错误页面 - 异常变量为空
【发布时间】:2014-04-06 15:29:27
【问题描述】:

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

<%= exception %> 

始终为空。

web.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>

error.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

【问题讨论】:

标签: jsp tomcat tomcat7


【解决方案1】:

改用表达式语言:

<%@ 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>

请注意,只有当它来自Exception 时,才会在生成 JSP 时打印异常,例如有一个 scriptlet 代码会引发未处理的Exception。如果它来自 400 或 500 HTTP 响应代码,则根本不会打印任何文本。

【讨论】:

    猜你喜欢
    • 2011-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-05
    • 1970-01-01
    • 2010-09-25
    • 2015-03-20
    • 2017-04-05
    相关资源
    最近更新 更多