gpt4 book ai didi

java - JSP异常处理

转载 作者:行者123 更新时间:2023-11-30 05:07:43 25 4
gpt4 key购买 nike

我有 2 个简单的 JSP 来检查发生异常时错误页面的导航:

calculation_page.jsp - 具有标记 - <%@ page errorPage="error_page.jsp"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<%-- include custom error page in this jsp code --%>
<%@ page errorPage="error_page.jsp" %>

<html>
<head>
<title>calculation page</title>
</head>
<body>
<%
int i = 10;

// This line will create an error so error page will be called
i = i / 0;
%>
</body>
</html>

error_page.jsp - 具有标记的错误页面 - <%@ page isErrorPage="true"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">

<%@ page isErrorPage = "true"%>
<%@ page import = "java.io.*" %>

<HTML>
<HEAD>
<TITLE> custom error page </TITLE>
</head>
<body>
<h2>Your application has generated an error</h2>
<h3>Please check for the error given below</h3>
<b>Exception:</b><br>
<font color="red"><%= exception.toString() %></font>
</body>
</html>

但是当发生异常时,它不会从第一页转到第二页,而是给出错误 - “HTTP 500 内部服务器错误”,并且浏览器显示:“网站无法显示该页面”。

请帮忙。

最佳答案

您的错误页面看起来正常,但您的 errorGenerator 页面应如下所示:

<%@page errorPage="/errorPage.jsp" contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
int i = 10;
i = i / 0;
%>
</body>
</html>

注意:页面指令中的errorPage="/errorPage.jsp"

页面指令的 errorPage 属性指定用作错误页面的备用页面,

关于java - JSP异常处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4538081/

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