1、首先配置web.xml 添加一下代码
500
/error.jsp
2、error.jsp页面
response.setStatus(HttpServletResponse.SC_OK);
%>
错误页面function showErrorMessage(){
$("#errorMessageDiv").toggle();
}
$(document).ready(showErrorMessage);
错误提示 | ||
| 尊敬的用户: 系统出现了异常,请重试。
|
try {
//全部内容先写到内存,然后分别从两个输出流再输出到页面和文件
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(byteArrayOutputStream);
printStream.println();
printStream.println("用户信息");
printStream.println("账号:" + request.getSession().getAttribute("userName"));
printStream.println("访问的路径: " + request.getAttribute("javax.servlet.forward.request_uri"));
printStream.println();
printStream.println("异常信息");
printStream.println(exception.getClass() + " : " + exception.getMessage());
printStream.println();
Enumeration e = request.getParameterNames();
if (e.hasMoreElements()) {
printStream.println("请求中的Parameter包括:");
while (e.hasMoreElements()) {
String key = e.nextElement();
printStream.println(key + "=" + request.getParameter(key));
}
printStream.println();
}
printStream.println("堆栈信息");
exception.printStackTrace(printStream);
printStream.println();
out.print(byteArrayOutputStream); //输出到网页
File dir = new File(request.getRealPath("/errorLog"));
if (!dir.exists()) {
dir.mkdir();
}
String timeStamp = new SimpleDateFormat("yyyyMMddhhmmssS").format(new Date());
FileOutputStream fileOutputStream = new FileOutputStream(new File(dir.getAbsolutePath() + File.separatorChar + "error-" + timeStamp + ".txt"));
new PrintStream(fileOutputStream).print(byteArrayOutputStream); //写到文件
} catch (Exception ex) {
ex.printStackTrace();
}
%>