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

web.xml 

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>  




JSP页面的关键在于 

1 isErrorPage="true" 

2 response.setStatus(HttpServletResponse.SC_OK); 

building.jsp 

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



error.jsp 

Java代码   收藏代码
  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.   
  5.       %>  
  6. <body>  
  7. 程序发生了错误,有可能该页面正在调试或者是设计上的缺陷.<br/>  
  8. 你可以选择<br/> <a href=<%=request.getContextPath()+"/forum/new.jsp" %>>反馈</a>  
  9. 提醒我... 或者<br/><a href="javascript:history.go(-1)">返回上一页</a>  
  10. <hr width=80%>  
  11. <h2><font color=#DB1260>JSP Error Page</font></h2>  
  12.   
  13. <p>An exception was thrown: <b> <%=exception.getClass()%>:<%=exception.getMessage()%></b></p>  
  14. <%  
  15. System.out.println("Header....");  
  16. Enumeration<String> e = request.getHeaderNames();  
  17. String key;  
  18. while(e.hasMoreElements()){  
  19.   key = e.nextElement();  
  20.   System.out.println(key+"="+request.getHeader(key));  
  21. }  
  22. System.out.println("Attribute....");  
  23. e = request.getAttributeNames();  
  24. while(e.hasMoreElements()){  
  25.   key = e.nextElement();  
  26.   System.out.println(key+"="+request.getAttribute(key));  
  27. }  
  28.   
  29. System.out.println("Parameter....");  
  30. e = request.getParameterNames();  
  31. while(e.hasMoreElements()){  
  32.   key = e.nextElement();  
  33.   System.out.println(key+"="+request.getParameter(key));  
  34. }  
  35. %>  
  36. 111<%=request.getAttribute("javax.servlet.forward.request_uri") %><br>  
  37. <%=request.getAttribute("javax.servlet.forward.servlet_path") %>  
  38.   
  39. <p>With the following stack trace:</p>  
  40. <pre>  
  41. <%exception.printStackTrace();  
  42.       ByteArrayOutputStream ostr = new ByteArrayOutputStream();  
  43.       exception.printStackTrace(new PrintStream(ostr));  
  44.       out.print(ostr);  
  45.     %>  
  46. </pre>  
  47. <hr width=80%>  
  48. </body>  



转自:http://blog.csdn.net/java2000_net/archive/2007/12/29/2000965.aspx

猜你喜欢

转载自bigdragon.iteye.com/blog/2268283