09-01
23
web.xml 配置404和500错误的自定义页面
作者:Java伴侣 日期:2009-01-23
web.xml 404和500错误配置示例Xml代码
JSP错误页面配置关键在于:
error.jspHtml代码
复制内容到剪贴板 程序代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http:java.sun.com/xml/ns/j2ee" xmlns:xsi="http:www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http:java.sun.com/xml/ns/j2ee http:java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<error-page>
<error-code>404</error-code>
<location>/building.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>
</web-app>
<web-app version="2.4" xmlns="http:java.sun.com/xml/ns/j2ee" xmlns:xsi="http:www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http:java.sun.com/xml/ns/j2ee http:java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<error-page>
<error-code>404</error-code>
<location>/building.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>
</web-app>
JSP错误页面配置关键在于:
复制内容到剪贴板 程序代码
<%@ page language="java" contentType="text/html; charset=utf-8" isErrorPage="true" pageEncoding="utf-8"%>
<%response.setStatus(HttpServletResponse.SC_OK);%>
<body>
您访问的页面不存在,<a href="javascript:history.go(-1)">返回</a>
</body>
<%response.setStatus(HttpServletResponse.SC_OK);%>
<body>
您访问的页面不存在,<a href="javascript:history.go(-1)">返回</a>
</body>
error.jspHtml代码
复制内容到剪贴板 程序代码
程序发生了错误,可能该页面正在调试或者是设计上的缺陷。<br/>
<hr width=80%>
<h2><font color=#DB1260>JSP Error Page</font></h2>
<p>An exception was thrown: <b> <%=exception.getClass()%>:<%=exception.getMessage()%></b></p>
<%
Enumeration<String> e = request.getHeaderNames();
String key;
while(e.hasMoreElements()){
key = e.nextElement();
}
e = request.getAttributeNames();
while(e.hasMoreElements()){
key = e.nextElement();
}
e = request.getParameterNames();
while(e.hasMoreElements()){
key = e.nextElement();
}
%>
<%=request.getAttribute("javax.servlet.forward.request_uri") %><br>
<%=request.getAttribute("javax.servlet.forward.servlet_path") %>
<p>With the following stack trace:</p>
<pre>
<%
exception.printStackTrace();
ByteArrayOutputStream ostr = new ByteArrayOutputStream();
exception.printStackTrace(new PrintStream(ostr));
out.print(ostr);
%>
</pre>
<hr width=80%>
</body>
<hr width=80%>
<h2><font color=#DB1260>JSP Error Page</font></h2>
<p>An exception was thrown: <b> <%=exception.getClass()%>:<%=exception.getMessage()%></b></p>
<%
Enumeration<String> e = request.getHeaderNames();
String key;
while(e.hasMoreElements()){
key = e.nextElement();
}
e = request.getAttributeNames();
while(e.hasMoreElements()){
key = e.nextElement();
}
e = request.getParameterNames();
while(e.hasMoreElements()){
key = e.nextElement();
}
%>
<%=request.getAttribute("javax.servlet.forward.request_uri") %><br>
<%=request.getAttribute("javax.servlet.forward.servlet_path") %>
<p>With the following stack trace:</p>
<pre>
<%
exception.printStackTrace();
ByteArrayOutputStream ostr = new ByteArrayOutputStream();
exception.printStackTrace(new PrintStream(ostr));
out.print(ostr);
%>
</pre>
<hr width=80%>
</body>
评论: 0 | 引用: 0 | 查看次数: 578
发表评论