歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java Web程序中error頁面處理

Java Web程序中error頁面處理

日期:2017/3/1 10:31:23   编辑:Linux編程

01)web.xml配置

定義錯誤頁面的位置,按錯誤碼不同定位到不同的錯誤展示頁面,系統中分為兩類錯誤,第一類是404頁面不存在的錯誤,另一類是服務器內部錯誤50x,對應的頁面分別為404.jsp和error.jsp

<error-page>

<error-code>500</error-code>

<location>/error.jsp</location>

</error-page>

<error-page>

<error-code>404</error-code>

<location>/404.jsp</location>

</error-page>

<error-page>

<error-code>403</error-code>

<location>/error.jsp</location>

</error-page>

02)錯誤處理頁面

404.jsp--友好提示用戶頁面不存在,這個頁面沒什麼特殊,核心是再該頁面上給用戶提示文案即可

<%@ page contentType="text/html; charset=UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>異常信息</title>
<style type="text/css">
body{ font-size:12px; color:#4d4b4b;}
</style>
</head>
<body>
<br><br><br><br><br><br>
<table width="700" border="1" cellpadding="0" cellspacing="0" align="center">
<tr>
<td colspan="2" valign="top" bgcolor="#D6E2F2" class="style01 tdpadding">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="25" class="img01 titlefalse">請求鏈接錯誤</td>
</tr>
<tr>
<td height="119" bgcolor="#FFFFFF" class="style01">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="20%" height="24" class="title01" align="center">
您訪問的頁面不存在。
</td>
</tr>

</table>
</td>
</tr>
</table>
</td>
</tr>
</table>

</body>
</html>

error.jsp--–對用戶友好提示,將錯誤信息隱藏在頁面中,可以通過CTRL+SHIFT+D鍵查看

<%@ page contentType="text/html; charset=UTF-8"%>
<%!String exceptionMsgForInner(Throwable e){
String ls_ErrMsg = e.getLocalizedMessage();
if (ls_ErrMsg == null) ls_ErrMsg = "";
ls_ErrMsg += "\r\n";
Throwable eCause = e.getCause();
if (eCause == null){
for (int ii = 0;ii < e.getStackTrace().length;ii++){
ls_ErrMsg += e.getStackTrace()[ii].toString() + "\r\n";
}
} else{
ls_ErrMsg += exceptionMsgForInner(eCause);
}
return ls_ErrMsg.trim();
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>異常信息</title>
<style type="text/css">
body{ font-size:12px; color:#4d4b4b;}
</style>
<SCRIPT LANGUAGE="JavaScript">
<!--
function dokeydown(){
if (event.ctrlKey && event.shiftKey && event.keyCode==68){
var obj=document.getElementById("divexception");
if (obj.style.display.toLowerCase()=="none"){
obj.style.display="block";
}else{
obj.style.display="none";
}
return false;
}
}
window.document.attachEvent('onkeydown', dokeydown);
//-->
</SCRIPT>
</head>
<body>
<br><br><br><br><br><br>
<table width="700" border="1" cellpadding="0" cellspacing="0" align="center">
<tr>
<td colspan="2" valign="top" bgcolor="#D6E2F2" class="style01 tdpadding">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="25" class="img01 titlefalse">異常信息</td>
</tr>
<tr>
<td height="119" bgcolor="#FFFFFF" class="style01">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="20%" height="24" class="title01" align="center">
系統異常,請聯系管理員。
</td>
</tr>

</table>
</td>
</tr>
<tr><td>
<div align=center id=divexception>
<TEXTAREA NAME="" ROWS="8" COLS="100">
<%
Exception ex = (Exception) request.getAttribute("javax.servlet.error.exception");
out.println(exceptionMsgForInner(ex));
%>
</TEXTAREA>
<div>
</td></tr>
</table>
</td>
</tr>
</table>

</body>
</html>

03)struts2中記錄異常堆棧到日志文件中

在struts.xml中添加exception攔截器,並且將日志的log功能打開,設置為ERROR,這樣在系統出現異常時,利用struts2的攔截器可以把異常詳細堆棧信息記錄到Log文件中,然後再將頁面跳轉到我們定義好的出錯頁面給用戶提示

<interceptor-stack name="defaultStack">

<interceptor-ref name="exception">

<param name="logEnabled">true</param>

<param name="logLevel">ERROR</param>

</interceptor-ref>

Copyright © Linux教程網 All Rights Reserved