⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 0170.htm

📁 JspServlet教程专栏 对javaservlet讲述的非常详细
💻 HTM
字号:
<html>

<head>
<title>新时代软件教程:操作系统 主页制作 服务器 设计软件 网络技术 编程语言 文字编辑</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style>
<!--
body, table {font-size: 9pt; font-family: 宋体}
a {text-decoration:none}
a:hover {color: red;text-decoration:underline}
.1  {background-color: rgb(245,245,245)}
-->
</style>
</head>
<p align="center"><script src="../../1.js"></script></a>
<p align="center"><big><strong>错误处理的例子</strong></big></p>
<div align="right">---摘自互联网</div>

<br>&lt;!--<br>
errorHandler.jsp<br>
<br>
This&nbsp; JSP&nbsp; is&nbsp; invoked&nbsp; if&nbsp; the&nbsp; following&nbsp; conditions&nbsp; are&nbsp; met:<br>
<br>
1.&nbsp; An&nbsp; exception&nbsp; type&nbsp; or&nbsp; an&nbsp; HTTP&nbsp; Status&nbsp; code&nbsp; is&nbsp; mapped<br>
to&nbsp; this&nbsp; JSP&nbsp; using&nbsp; the&nbsp; Error&nbsp; Pages&nbsp; page&nbsp; of&nbsp; the<br>
Web&nbsp; Application&nbsp; Admin&nbsp; UI<br>
<br>
2.&nbsp; The&nbsp; specified&nbsp; Status&nbsp; Code&nbsp; is&nbsp; returned&nbsp; upon&nbsp; accessing&nbsp; a&nbsp; resource<br>
in&nbsp; the&nbsp; Web&nbsp; Application,&nbsp; OR&nbsp; the&nbsp; specifed&nbsp; Exception&nbsp; type&nbsp; is&nbsp; thrown<br>
as&nbsp; a&nbsp; result&nbsp; of&nbsp; processing&nbsp; a&nbsp; SERVLET&nbsp; request&nbsp; (not&nbsp; a&nbsp; JSP&nbsp; request).<br>
<br>
NOTE:&nbsp; In&nbsp; the&nbsp; case&nbsp; that&nbsp; a&nbsp; JSP&nbsp; page&nbsp; throws&nbsp; the&nbsp; specified&nbsp; Exception<br>
(rather&nbsp; than&nbsp; a&nbsp; Servlet),&nbsp; the&nbsp; error&nbsp; handling&nbsp; mechanism&nbsp; is<br>
completely&nbsp; unrelated&nbsp; to&nbsp; the&nbsp; settings&nbsp; on&nbsp; the&nbsp; Error&nbsp; Pages&nbsp; page<br>
of&nbsp; the&nbsp; Web&nbsp; Application&nbsp; admin&nbsp; UI.<br>
<br>
Instead&nbsp; the&nbsp; following&nbsp; scheme&nbsp; is&nbsp; used:<br>
<br>
1.&nbsp; The&nbsp; offending&nbsp; JSP&nbsp; names&nbsp; the&nbsp; error&nbsp; handling&nbsp; JSP&nbsp; as&nbsp; being&nbsp; it's<br>
error&nbsp; page&nbsp; using&nbsp; a&nbsp; page&nbsp; directive.<br>
<br>
2.&nbsp; The&nbsp; error&nbsp; handling&nbsp; JSP&nbsp; must&nbsp; declare&nbsp; itself&nbsp; to&nbsp; be&nbsp; an&nbsp; <br>
errorpage&nbsp; by&nbsp; using&nbsp; a&nbsp; page&nbsp; directive.<br>
<br>
See&nbsp; the&nbsp; top&nbsp; of&nbsp; p.&nbsp; 33&nbsp; of&nbsp; the&nbsp; JSP&nbsp; 1.0&nbsp; Specification&nbsp; for&nbsp; more&nbsp; information<br>
regarding&nbsp; this&nbsp; mechanism.<br>
<br>
<br>
Whenever&nbsp; this&nbsp; page&nbsp; is&nbsp; invoked,&nbsp; it&nbsp; tries&nbsp; to&nbsp; tell&nbsp; you&nbsp; what&nbsp; the&nbsp; error&nbsp; condition&nbsp; is<br>
(HTTP&nbsp; Status&nbsp; Code&nbsp; or&nbsp; Exception).&nbsp; If&nbsp; it&nbsp; cannot&nbsp; determine&nbsp; the&nbsp; condition,&nbsp; it&nbsp; will<br>
tell&nbsp; you&nbsp; that&nbsp; it&nbsp; could&nbsp; not&nbsp; determine&nbsp; the&nbsp; condition.&nbsp; Therefore,&nbsp; invoking&nbsp; it&nbsp; directly&nbsp; from<br>
your&nbsp; browser&nbsp; is&nbsp; not&nbsp; really&nbsp; useful.<br>
--><br>
<br>
<br>
&lt;!--<br>
The&nbsp; following&nbsp; directive&nbsp; allows&nbsp; this&nbsp; JSP&nbsp; to&nbsp; access&nbsp; an&nbsp; Exception&nbsp; that&nbsp; may<br>
have&nbsp; been&nbsp; thrown&nbsp; by&nbsp; another&nbsp; JSP.<br>
--><br>
&lt;%@&nbsp; page&nbsp; isErrorPage="true"&nbsp; %><br>
<br>
&lt;html><br>
&lt;head><br>
&lt;title>Error&nbsp; Handling&nbsp; JSP&lt;/title><br>
<br>
&lt;style&nbsp; type="TEXT/CSS"><br>
&lt;!--<br>
H2,&nbsp; H3,&nbsp; H4&nbsp; {&nbsp; font-family:&nbsp; Verdana,&nbsp; Arial,&nbsp; sans-serif&nbsp; }<br>
TH&nbsp; {&nbsp; font-family:&nbsp; Verdana,&nbsp; Arial,&nbsp; sans-serif;&nbsp; font-size:&nbsp; 10pt;&nbsp; font-weight:&nbsp; bold&nbsp; }<br>
.headerText&nbsp; {&nbsp; font-family:&nbsp; Verdana,&nbsp; Arial,&nbsp; sans-serif;&nbsp; font-size:&nbsp; 10pt;&nbsp; font-weight:&nbsp; bold&nbsp; }<br>
P,&nbsp; UL,&nbsp; LI,&nbsp; BLOCKQUOTE&nbsp; {&nbsp; font-family:&nbsp; Verdana,&nbsp; Arial,&nbsp; sans-serif;&nbsp; font-size:&nbsp; 10pt&nbsp; }<br>
.normalText&nbsp; {&nbsp; font-family:&nbsp; Verdana,&nbsp; Arial,&nbsp; sans-serif;&nbsp; font-size:&nbsp; 10pt&nbsp; }<br>
.alertText&nbsp; {&nbsp; font-family:&nbsp; Verdana,&nbsp; Arial,&nbsp; sans-serif;&nbsp; font-size:&nbsp; 10pt;&nbsp; font-weight:&nbsp; bold;&nbsp; color:&nbsp; red&nbsp; }<br>
.errorText&nbsp; {&nbsp; font-family:&nbsp; Verdana,&nbsp; Arial,&nbsp; sans-serif;&nbsp; font-size:&nbsp; 14pt;&nbsp; font-weight:&nbsp; bold;&nbsp; color:&nbsp; red&nbsp; }<br>
.infoText&nbsp; {&nbsp; font-family:&nbsp; Verdana,&nbsp; Arial,&nbsp; sans-serif;&nbsp; font-size:&nbsp; 14pt;&nbsp; font-weight:&nbsp; bold;&nbsp; color:&nbsp; green&nbsp; }<br>
--><br>
&lt;/style><br>
&lt;head><br>
<br>
&lt;%<br>
Integer&nbsp; statusCode&nbsp; =&nbsp; null;<br>
String&nbsp; message&nbsp; =&nbsp; null;<br>
Exception&nbsp; excep&nbsp; =&nbsp; null;&nbsp; <br>
Throwable&nbsp; t&nbsp; =&nbsp; null;&nbsp; //for&nbsp; Exceptions&nbsp; thrown&nbsp; by&nbsp; another&nbsp; JSP<br>
<br>
//try&nbsp; to&nbsp; get&nbsp; the&nbsp; HTTP&nbsp; status&nbsp; code&nbsp; OR&nbsp; the&nbsp; Exception&nbsp; thrown&nbsp; by&nbsp; a&nbsp; Servlet<br>
statusCode&nbsp; =&nbsp; (Integer)&nbsp; request.getAttribute("javax.servlet.error.status_code");<br>
message&nbsp; =&nbsp; (String)&nbsp; request.getAttribute("javax.servlet.error.message");<br>
excep&nbsp; =&nbsp; (Exception)&nbsp; request.getAttribute("javax.servlet.error.exception_type");<br>
<br>
String&nbsp; problem,&nbsp; explanation;<br>
<br>
//try&nbsp; to&nbsp; get&nbsp; the&nbsp; exception&nbsp; thrown&nbsp; by&nbsp; a&nbsp; JSP&nbsp; (note:&nbsp; the&nbsp; implicit&nbsp; "exception"&nbsp; could&nbsp; be&nbsp; used&nbsp; here&nbsp; instead)<br>
t&nbsp; =&nbsp; (Throwable)&nbsp; request.getAttribute("javax.servlet.jsp.jspException");<br>
<br>
if(statusCode&nbsp; !=&nbsp; null)<br>
{<br>
message&nbsp; =&nbsp; (message&nbsp; ==&nbsp; null&nbsp; ?&nbsp; "No&nbsp; description&nbsp; available."&nbsp; :&nbsp; message);<br>
<br>
problem&nbsp; =&nbsp; "HTTP&nbsp; Status&nbsp; Code&nbsp; -&nbsp; "&nbsp; +statusCode.intValue();<br>
explanation&nbsp; =&nbsp; message;<br>
}<br>
<br>
//process&nbsp; an&nbsp; exception&nbsp; thrown&nbsp; by&nbsp; a&nbsp; Servlet&nbsp; (not&nbsp; a&nbsp; JSP)<br>
else&nbsp; if(excep&nbsp; !=&nbsp; null)<br>
{<br>
problem&nbsp; =&nbsp; "An&nbsp; Exception&nbsp; was&nbsp; thrown&nbsp; while&nbsp; trying&nbsp; to&nbsp; process&nbsp; your&nbsp; request";<br>
explanation&nbsp; =&nbsp; excep.toString();<br>
}<br>
<br>
//process&nbsp; an&nbsp; exception&nbsp; thrown&nbsp; by&nbsp; another&nbsp; JSP&nbsp; (note:&nbsp; here&nbsp; is&nbsp; where&nbsp; a&nbsp; test&nbsp; using&nbsp; the&nbsp; implicit&nbsp; "exception"<br>
//such&nbsp; as&nbsp; exception&nbsp; !=&nbsp; null,&nbsp; followed&nbsp; by&nbsp; explanation&nbsp; =&nbsp; exception.getMessage()&nbsp; could&nbsp; be&nbsp; used&nbsp; instead).<br>
<br>
else&nbsp; if(t&nbsp; !=&nbsp; null)<br>
{<br>
problem&nbsp; =&nbsp; "An&nbsp; Exception&nbsp; was&nbsp; thrown&nbsp; while&nbsp; trying&nbsp; to&nbsp; process&nbsp; your&nbsp; request";<br>
explanation&nbsp; =&nbsp; t.toString();<br>
}<br>
<br>
else<br>
{<br>
problem&nbsp; =&nbsp; "Unknown";<br>
explanation&nbsp; =&nbsp; "The&nbsp; exact&nbsp; cause&nbsp; of&nbsp; the&nbsp; error&nbsp; could&nbsp; not&nbsp; be&nbsp; determined";<br>
}<br>
%><br>
<br>
&lt;body&nbsp; bgcolor=#ffffff><br>
&lt;div&nbsp; style="margin:&nbsp; 2%"><br>
&lt;p&nbsp; class="errorText"><br>
!An&nbsp; Error&nbsp; Occurred&nbsp; while&nbsp; processing&nbsp; your&nbsp; request.<br>
&lt;/p><br>
<br>
&lt;p>&lt;/p><br>
<br>
<br>
&lt;table&nbsp; border=1&nbsp; width="96%"&nbsp; cellspacing=2&nbsp; cellpadding=6&nbsp; bgcolor=#ffcc66><br>
&lt;tr><br>
&lt;th>Problem&lt;/th><br>
&lt;th>Explanation&lt;/th><br>
&lt;/tr><br>
<br>
&lt;tr><br>
&lt;td&nbsp; align="center"&nbsp; bgcolor=#ffffee&nbsp; class="normalText"><br>
&lt;%=problem%><br>
&lt;/td><br>
<br>
&lt;td&nbsp; bgcolor=#ffffee&nbsp; class="normalText"><br>
&lt;%=explanation%><br>
&lt;/td><br>
&lt;/tr><br>
<br>
&lt;/table>&nbsp; <br>
<br>
&lt;p>Go&nbsp; to&nbsp; the&nbsp; &lt;a&nbsp; href="&lt;%=request.getContextPath()&nbsp; +&nbsp; "/admin"%>">admin&nbsp; UI&nbsp; for&nbsp; &lt;%=request.getContextPath().substring(1)%>&lt;/a><br>
&lt;/div><br>
&lt;/body><br>
<br>
&lt;/html>

  </table>
<p align="center"><script src="../../2.js"></script></a>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -