JSP异常处理 [英] JSP Exception Handling
问题描述
我有2个简单的JSP,可在出现异常时检查导航到错误页面:
I have 2 simple JSP to check navigation to error page on occurrence of exceptions:
calculation_page.jsp-具有标签-<%@ page errorPage="error_page.jsp"%>
calculation_page.jsp -having the tag - <%@ 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-具有标签的错误页面-<%@页面isErrorPage ="true"%>
error_page.jsp - error page having the tag - <%@ 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 Internal Server Error",浏览器显示:网站无法显示该页面".
But when an exception occurs, instead of going from 1st page to the 2nd one, it gives an error – "HTTP 500 Internal Server Error" and the browser says: "The website cannot display the page".
请帮助.
推荐答案
您的错误页面看起来 OK ,但是您的errorGenerator页面应该看起来像:
your error page looks OK but your errorGenerator page should look like :
<%@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>
注意:页面指令
page指令的errorPage属性指定用作错误页面的备用页面,
这篇关于JSP异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!