错误处理的例子

错误处理的例子

---摘自互联网

<!--
errorHandler.jsp

This JSP is invoked if the following conditions are met:

1. An exception type or an HTTP Status code is mapped
to this JSP using the Error Pages page of the
Web Application Admin UI

2. The specified Status Code is returned upon accessing a resource
in the Web Application, OR the specifed Exception type is thrown
as a result of processing a SERVLET request (not a JSP request).

NOTE: In the case that a JSP page throws the specified Exception
(rather than a Servlet), the error handling mechanism is
completely unrelated to the settings on the Error Pages page
of the Web Application admin UI.

Instead the following scheme is used:

1. The offending JSP names the error handling JSP as being it's
error page using a page directive.

2. The error handling JSP must declare itself to be an
errorpage by using a page directive.

See the top of p. 33 of the JSP 1.0 Specification for more information
regarding this mechanism.


Whenever this page is invoked, it tries to tell you what the error condition is
(HTTP Status Code or Exception). If it cannot determine the condition, it will
tell you that it could not determine the condition. Therefore, invoking it directly from
your browser is not really useful.
-->


<!--
The following directive allows this JSP to access an Exception that may
have been thrown by another JSP.
-->
<%@ page isErrorPage="true" %>

<html>
<head>
<title>Error Handling JSP</title>

<style type="TEXT/CSS">
<!--
H2, H3, H4 { font-family: Verdana, Arial, sans-serif }
TH { font-family: Verdana, Arial, sans-serif; font-size: 10pt; font-weight: bold }
.headerText { font-family: Verdana, Arial, sans-serif; font-size: 10pt; font-weight: bold }
P, UL, LI, BLOCKQUOTE { font-family: Verdana, Arial, sans-serif; font-size: 10pt }
.normalText { font-family: Verdana, Arial, sans-serif; font-size: 10pt }
.alertText { font-family: Verdana, Arial, sans-serif; font-size: 10pt; font-weight: bold; color: red }
.errorText { font-family: Verdana, Arial, sans-serif; font-size: 14pt; font-weight: bold; color: red }
.infoText { font-family: Verdana, Arial, sans-serif; font-size: 14pt; font-weight: bold; color: green }
-->
</style>
<head>

<%
Integer statusCode = null;
String message = null;
Exception excep = null;
Throwable t = null; //for Exceptions thrown by another JSP

//try to get the HTTP status code OR the Exception thrown by a Servlet
statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
message = (String) request.getAttribute("javax.servlet.error.message");
excep = (Exception) request.getAttribute("javax.servlet.error.exception_type");

String problem, explanation;

//try to get the exception thrown by a JSP (note: the implicit "exception" could be used here instead)
t = (Throwable) request.getAttribute("javax.servlet.jsp.jspException");

if(statusCode != null)
{
message = (message == null ? "No description available." : message);

problem = "HTTP Status Code - " +statusCode.intValue();
explanation = message;
}

//process an exception thrown by a Servlet (not a JSP)
else if(excep != null)
{
problem = "An Exception was thrown while trying to process your request";
explanation = excep.toString();
}

//process an exception thrown by another JSP (note: here is where a test using the implicit "exception"
//such as exception != null, followed by explanation = exception.getMessage() could be used instead).

else if(t != null)
{
problem = "An Exception was thrown while trying to process your request";
explanation = t.toString();
}

else
{
problem = "Unknown";
explanation = "The exact cause of the error could not be determined";
}
%>

<body bgcolor=#ffffff>
<div style="margin: 2%">
<p class="errorText">
!An Error Occurred while processing your request.
</p>

<p></p>


<table border=1 width="96%" cellspacing=2 cellpadding=6 bgcolor=#ffcc66>
<tr>
<th>Problem</th>
<th>Explanation</th>
</tr>

<tr>
<td align="center" bgcolor=#ffffee class="normalText">
<%=problem%>
</td>

<td bgcolor=#ffffee class="normalText">
<%=explanation%>
</td>
</tr>

</table>

<p>Go to the <a href="<%=request.getContextPath() + "/admin"%>">admin UI for <%=request.getContextPath().substring(1)%></a>
</div>
</body>

</html>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值