您的位置:首页 > Web前端 > JavaScript

JSP定义错误页面,并在页面中点击按钮显示隐藏错误信息

2016-05-31 10:10 405 查看
1、现在web.xml中添加错误页面配置

<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>


2、在WebContent下定义错误页面error.jsp

<%@ page language="java" contentType="text/html; charset=gbk" pageEncoding="gbk"%>
<%@ page import="java.text.SimpleDateFormat"%>
<%@ page isErrorPage="true" %>
<%@ page import="java.io.*"%>
<%@ page language="java" import="java.util.*" %>
<%
response.setStatus(HttpServletResponse.SC_OK);
String path = request.getContextPath();
String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path;
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<script type="text/javascript" src="<%=path%>/js/jquery-1.7.2.js"></script>
<head>
<title>错误页面.</title>
</head>
<body>
<table width="100%">
<tr>
<td style="border-bottom:dotted 1px Gray;" colspan="2" >
  错误提示
</td><td></td>
</tr>
<tr>
<td style="width: 130px" >
<img src="<%=path%>/img/error.png" id="error_img" />
</td>
<td>尊敬的用户:<br />系统出现了异常,请重新登录或刷新页面。
<br />如果问题重复出现,请向信管部反馈。<br /><br />
<a id="showErrorMessageButton" href="javascript:showErrorMessage();">详细错误信息</a>
</td>
</tr>
</table>
<div id="errorMessageDiv">
<pre>
<%
try {
//全部内容先写到内存,然后分别从两个输出流再输出到页面和文件
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(byteArrayOutputStream);

printStream.println();
printStream.println("用户信息");
printStream.println("访问的路径: " + request.getAttribute("javax.servlet.forward.request_uri"));
printStream.println();

printStream.println("异常信息");
printStream.println(exception.getClass() + " : " + exception.getMessage());
printStream.println();

Enumeration<String> 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); //输出到网页

} catch (Exception ex) {
ex.printStackTrace();
}
%>
</pre>
</div>
<script>
function showErrorMessage(){
$("#errorMessageDiv").toggle();
}
$(document).ready(function(){
showErrorMessage();
});
</script>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: