Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1606086
  • 博文数量: 695
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 4027
  • 用 户 组: 普通用户
  • 注册时间: 2013-11-20 21:22
文章分类

全部博文(695)

文章存档

2018年(18)

2017年(74)

2016年(170)

2015年(102)

2014年(276)

2013年(55)

分类: Java

2016-09-27 09:57:01

1.需要在web.xml中配置相关信息

点击(此处)折叠或打开


  1.     <error-page>
  2.         <error-code>403</error-code>
  3.         <location>/403.html</location>
  4.     </error-page>
  5.     <error-page>
  6.         <error-code>404</error-code>
  7.         <location>/404.html</location>
  8.     </error-page>
  9.     
  10.     
  11.     <error-page>
  12.         <error-code>500</error-code>
  13.         <location>/500.html</location>
  14.     </error-page>
  15.     
  16.     
  17.     <error-page>
  18.         <exception-type>java.lang.Exception</exception-type>
  19.         <location>/500.jsp</location>
  20.     </error-page>
  21.     
  22.     <error-page>
  23.         <exception-type>java.lang.Throwable</exception-type>
  24.         <location>/500.jsp</location>
  25.     </error-page>
  26.     
2.如果配置是html时,不用另做配置

   如果配置是Jsp时,需要把isErrorPage设置为true,

   及<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8" isErrorPage="true"%>

3.获取异常信息及输出

点击(此处)折叠或打开

  1. <%@page import="java.io.PrintStream"%>
  2. <%@page import="java.io.ByteArrayOutputStream"%>
  3. <%@ include file="WEB-INF/views/includes/tags.jsp"%>
  4. <%@ page language="java" contentType="text/html; charset=UTF-8"
  5.     pageEncoding="UTF-8" isErrorPage="true"%>
  6. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
  7. <html>
  8. <head>
  9. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  10. <title>500 服务器内部错误</title>
  11. </head>
  12. <body>
  13.  <div class="ui-alert-panel">
  14.         <h1>服务器内部错误</h1>
  15.         <p>处理您的请求时发生错误!请确认您通过正确途径操作。</p>
  16.     </div>
  17.   <div style="display:none;">
  18.   <% //此处输出异常信息
  19.        exception.printStackTrace();

  20.      ByteArrayOutputStream ostr = new ByteArrayOutputStream();
  21.       exception.printStackTrace(new PrintStream(ostr));
  22.       out.print(ostr);
  23.   %>
  24.   </div>
  25. </body>
  26. </html>


阅读(2470) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~