web.xml配置错误页面,及输出错误信息

来源:互联网 发布:软件测试并发测试 编辑:程序博客网 时间:2024/05/21 12:41
1.需要在web.xml中配置相关信息
<!-- 默认的错误处理页面 -->      <error-page>          <error-code>403</error-code>          <location>/403.html</location>      </error-page>      <error-page>          <error-code>404</error-code>          <location>/404.html</location>      </error-page>      <!-- 仅仅在调试的时候注视掉,在正式部署的时候不能注释 -->      <!-- 这样配置也是可以的,表示发生500错误的时候,转到500.jsp页面处理。 -->      <error-page>           <error-code>500</error-code>           <location>/500.html</location>       </error-page>             <!-- 这样的配置表示如果jsp页面或者servlet发生java.lang.Exception类型(当然包含子类)的异常就会转到500.jsp页面处理。 -->      <error-page>           <exception-type>java.lang.Exception</exception-type>           <location>/500.jsp</location>       </error-page>             <error-page>           <exception-type>java.lang.Throwable</exception-type>           <location>/500.jsp</location>       </error-page>      <!--       当error-code和exception-type都配置时,exception-type配置的页面优先级高      及出现500错误,发生异常Exception时会跳转到500.jsp       -->   


2.如果配置是html时,不用另做配置

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

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


3.获取异常信息及输出

<%@page import="java.io.PrintStream"%>  <%@page import="java.io.ByteArrayOutputStream"%>  <%@ include file="WEB-INF/views/includes/tags.jsp"%>  <%@ page language="java" contentType="text/html; charset=UTF-8"      pageEncoding="UTF-8" isErrorPage="true"%>  <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  <html>  <head>  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  <title>500 服务器内部错误</title>  </head>  <body>   <div class="ui-alert-panel">          <h1>服务器内部错误</h1>          <p>处理您的请求时发生错误!请确认您通过正确途径操作。</p>      </div>    <div style="display:none;">    <%  //此处输出异常信息        exception.printStackTrace();          ByteArrayOutputStream ostr = new ByteArrayOutputStream();        exception.printStackTrace(new PrintStream(ostr));        out.print(ostr);    %>    </div>  </body>  </html>  


0 0