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

 

web.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app version="2.4"
3      xmlns="http://java.sun.com/xml/ns/j2ee"
4      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5      xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
6      http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
7     
8 <error-page>
9     <error-code>404</error-code>
10     <location>/building.jsp</location>
11 </error-page>
12
13 <error-page>
14     <error-code>500</error-code>
15     <location>/error.jsp</location>
16 </error-page>
17     
18 </web-app>
19


JSP页面的关键在于
1 isErrorPage="true"
2 response.setStatus(HttpServletResponse.SC_OK);
building.jsp

1 <%@ page language="java" contentType="text/html; charset=GBK" isErrorPage="true" pageEncoding="GBK"%>
2 <%response.setStatus(HttpServletResponse.SC_OK);%>
3 <%
4 /** *//**
5 * 本页面是在客户查找的页面无法找到的情况下调用的
6 */
7 response.setStatus(HttpServletResponse.SC_OK);
8 %>
9 <body>
10 正在制作 <a href="javascript:history.go(-1)">返回</a>
11 <br/>
12 也可能页面连接更改了,请按 F5 键刷新整个页面看看,特别是菜单!
13 </body>

error.jsp
1 <%@ page language="java" contentType="text/html; charset=GBK" isErrorPage="true" pageEncoding="GBK"%>
2 <%@ page import="java.io.*,java.util.*"%>
3 <%response.setStatus(HttpServletResponse.SC_OK);%>
4 <body>
5 程序发生了错误,有可能该页面正在调试或者是设计上的缺陷.<br/>
6 你可以选择<br/> <a href=<%=request.getContextPath()+"/forum/new.jsp" %>>反馈</a>
7 提醒我 或者<br/><a href="javascript:history.go(-1)">返回上一页</a>
8 <hr width=80%>
9 <h2><font color=#DB1260>JSP Error Page</font></h2>
10 <p>An exception was thrown: <b> <%=exception.getClass()%>:<%=exception.getMessage()%></b></p>
11 <%
12 Enumeration<String> e = request.getHeaderNames();
13 String key;
14 while(e.hasMoreElements()) {
15    key = e.nextElement();
16 }
17 e = request.getAttributeNames();
18 while(e.hasMoreElements()) {
19    key = e.nextElement();
20 }
21 e = request.getParameterNames();
22 while(e.hasMoreElements()) {
23    key = e.nextElement();
24 }
25 %>
26 <%=request.getAttribute("javax.servlet.forward.request_uri") %><br>
27 <%=request.getAttribute("javax.servlet.forward.servlet_path") %>
28 <p>With the following stack trace:</p>
29 <pre>
30 <%
31    exception.printStackTrace();
32    ByteArrayOutputStream ostr = new ByteArrayOutputStream();
33    exception.printStackTrace(new PrintStream(ostr));
34    out.print(ostr);
35 %>
36 </pre>
37 <hr width=80%>
38 </body>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值