gpt4 book ai didi

JSP 自定义错误页面 - 异常变量为空

转载 作者:行者123 更新时间:2023-11-28 22:20:31 26 4
gpt4 key购买 nike

我正在为在 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>

错误.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 的 scriptlet 代码。如果它来自 400 或 500 HTTP 响应代码,它根本不会打印任何文本。

关于JSP 自定义错误页面 - 异常变量为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22183090/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com