09-01
23

web.xml 配置404和500错误的自定义页面

web.xml 404和500错误配置示例Xml代码


<?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>


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>


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>  


文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: 404 500
相关日志:
评论: 0 | 引用: 0 | 查看次数: 539
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭