我正在为在tomcat 7上运行的JSP应用程序创建一个简单的自定义错误页面。我在打印异常时遇到问题。打印时的异常变量:
<%= exception %>
始终为null。
<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>
<%@ 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
答案 0 :(得分: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>
请注意,只有在生成JSP时来自Exception
的情况下才会打印例外,例如有一个脚本代码,抛出一个未处理的Exception
。如果它来自400或500 HTTP响应代码,它将不会打印任何文本。