错误处理的例子

---摘自互联网

<!--
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>