<error-page> <error-code>500</error-code> <location>/error.jsp</location> </error-page>
/**
* 异常处理类
*/
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;
}
}
<!-- 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 style="padding:2% 0;text-indent:2em;">尊敬的用户:我们致力于提供更好的服务,但人算不如天算,有些错误发生了,希望是在控制的范围内……如果问题重复出现,请向系统管理员反馈。</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>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有