淺析Java Web毛病/異常處置頁面。本站提示廣大學習愛好者:(淺析Java Web毛病/異常處置頁面)文章只能為提供參考,不一定能成為您想要的結果。以下是淺析Java Web毛病/異常處置頁面正文
產生辦事器 500 異常,假如默許方法處置,則是將異常捕捉以後跳到 Tomcat 缺省的異常頁面,以下圖所示。
豈論哪一個網站都是一樣的,所認為了知足自界說的須要,Tomcat 也許可自界說款式的。也就是在 web.xml 文件中設置裝備擺設:
<error-page> <error-code>500</error-code> <location>/error.jsp</location> </error-page>
起首說說自帶的邏輯。假如某個 JSP 頁面在履行的進程中湧現了毛病, 那末 JSP 引擎會主動發生一個異常對象,假如這個 JSP 頁面指定了另外一個 JSP 頁面為毛病處置法式,那末 JSP 引擎會將這個異常對象放入到 request 對象中,傳到毛病處置法式中。假如年夜家有寫 Servlet 的印象,這是和誰人轉向模版 JSP 的 javax.servlet.forward.request_uri 一個思緒,保存了原要求的途徑而不是 JSP 頁面的誰人途徑。在毛病處置法式裡,由於 page 編譯指令的 isErrorPage 屬性的值被設為 true,那末 JSP 引擎會主動聲明一個 exception 對象,這個 exception 對象從 request 對象所包括的 HTTP 參數中取得。
request 對象中包括的異常信息異常豐碩,以下所示:
你可以用 Java 語句 request.getAttribute("javax.servlet.error.status_code") 獲得,也能夠在 JSP 頁面中經由過程 EL 表達式來獲得,如 ${requestScope["javax.servlet.error.status_code"]}。
這個自界說毛病頁面固然簡略,JSP 自己也有很好的封裝成果,我也看過他人很多的資本,但細究之下也有很多“學問”,因而我想從新再”磨磨這個輪子“——起首 location 是一個 jsp 頁面,也能夠是 servlet,不外萬一 servlet 也有能夠啟動不起來的話那就應用簡略的 JSP 頁面就行了。我們經由過程 JSP 頁面界說外部類的辦法,到達頁面與邏輯的分別(不必編寫 servlet)。其他的思緒以下:
在 JSP 外面完成 ErrorHandler 類,尚有頁面挪用這個 ErrorHandler 類
不只可以接收 JSP 頁面的毛病,也可接收 servlet 的掌握器傳遞的毛病,而且提取盡可能多信息
全體內容先寫到內存,然後分離從兩個輸入流再輸入到頁面和文件
把毛病信息輸入到網頁的同時,簡略加幾句話,可以把網頁上的信息也寫一份到數據庫或許文本
可以前往 HTML/JSON/XML
完成代碼以下:
/**
* 異常處置類
*/
class ErrorHandler {
// 全體內容先寫到內存,然後分離從兩個輸入流再輸入到頁面和文件
private ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
private PrintStream printStream = new PrintStream(byteArrayOutputStream);
/**
* 搜集毛病信息
* @param request
* @param exception
* @param out
*/
public ErrorHandler(HttpServletRequest request, Throwable exception, JspWriter out) {
setRequest(request);
setException(exception);
if(out != null) {
try {
out.print(byteArrayOutputStream); // 輸入到網頁
} catch (IOException e) {
e.printStackTrace();
}
}
log(request);
if(byteArrayOutputStream != null)
try {
byteArrayOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
if(printStream != null) printStream.close();
}
/**
*
* @param request
*/
private void setRequest(HttpServletRequest request) {
printStream.println();
printStream.println("用戶賬號:" + request.getSession().getAttribute("userName"));
printStream.println("拜訪的途徑: " + getInfo(request, "javax.servlet.forward.request_uri", String.class));
printStream.println("失足頁面地址: " + getInfo(request, "javax.servlet.error.request_uri", String.class));
printStream.println("毛病代碼: " + getInfo(request, "javax.servlet.error.status_code", int.class));
printStream.println("異常的類型: " + getInfo(request, "javax.servlet.error.exception_type", Class.class));
printStream.println("異常的信息: " + getInfo(request, "javax.servlet.error.message", String.class));
printStream.println("異常servlet: " + getInfo(request, "javax.servlet.error.servlet_name", String.class));
printStream.println();
// 別的兩個對象
getInfo(request, "javax.servlet.jspException", Throwable.class);
getInfo(request, "javax.servlet.forward.jspException", Throwable.class);
Map<String, String[]> map = request.getParameterMap();
for (String key : map.keySet()) {
printStream.println("要求中的 Parameter 包含:");
printStream.println(key + "=" + request.getParameter(key));
printStream.println();
}
for (Cookie cookie : request.getCookies()){ // cookie.getValue()
printStream.println("要求中的 Cookie 包含:");
printStream.println(cookie.getName() + "=" + cookie.getValue());
printStream.println();
}
}
/**
*
* @param exception
*/
private void setException(Throwable exception) {
if (exception != null) {
printStream.println("異常信息");
printStream.println(exception.getClass() + " : " + exception.getMessage());
printStream.println();
printStream.println("客棧信息");
exception.printStackTrace(printStream);
printStream.println();
}
}
/**
*
* @param request
*/
private void log(HttpServletRequest request) {
File dir = new File(request.getSession().getServletContext().getRealPath("/errorLog"));
if (!dir.exists()) {
dir.mkdir();
}
String timeStamp = new java.text.SimpleDateFormat("yyyyMMddhhmmssS").format(new Date());
File file = new File(dir.getAbsolutePath() + File.separatorChar + "error-" + timeStamp + ".txt");
// try(FileOutputStream fileOutputStream = new FileOutputStream(file);
// PrintStream ps = new PrintStream(fileOutputStream)){// 寫到文件
// ps.print(byteArrayOutputStream);
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// } catch (Exception e){
// e.printStackTrace();
// }
}
/**
*
* @param request
* @param key
* @param type
* @return
*/
@SuppressWarnings("unchecked")
private <T> T getInfo(HttpServletRequest request, String key, Class<T> type){
Object obj = request.getAttribute(key);
return obj == null ? null : (T) obj;
}
}
如許便可以完成異常的掌握了。上面界說 web.xml,讓 tomcat 失足引向我們適才指定的頁面 error.jsp
<!-- 404 頁面不存在毛病 --> <error-page> <error-code>404</error-code> <location>/WEB-INF/jsp/common/default/error.jsp</location> </error-page> <!-- // --> <!-- 500 辦事器外部毛病 --> <error-page> <error-code>500</error-code> <location>/WEB-INF/jsp/common/default/error.jsp</location> </error-page> <!-- // -->
我們支配一個默許的頁面以下
源碼以下:
<%@page pageEncoding="UTF-8" isErrorPage="true"%>
<%@ include file="/WEB-INF/jsp/common/ClassicJSP/util.jsp"%>
<!DOCTYPE html>
<html>
<head>
<title>毛病頁面</title>
<style>
body {
max-width: 600px;
min-width: 320px;
margin: 0 auto;
padding-top: 2%;
}
textarea {
width: 100%;
min-height: 300px;
}
h1 {
text-align: right;
color: lightgray;
}
div {
margin-top: 1%;
}
</style>
</head>
<body>
<h1>抱 歉!</h1>
<div >尊重的用戶:我們努力於供給更好的辦事,但千算萬算,不值天一劃,有些毛病產生了,願望是在掌握的規模內……假如成績反復湧現,請向體系治理員反應。</div>
<textarea><%
new ErrorHandler(request, exception, out);
%></textarea>
<div>
<center>
<a href="${pageContext.request.contextPath}">回想頁</a> | <a href="javascript:history.go(-1);">上一頁</a>
</center>
</div>
</body>
</html>
以上就是本文的全體內容,願望對年夜家的進修有所贊助。