浅析Java Web错误/异常处理页面


发生服务器 500 异常,如果默认方式处理,则是将异常捕获之后跳到 Tomcat 缺省的异常页面,如下图所示。

不论哪个网站都是一样的,所以为了满足自定义的需要,Tomcat 也允许自定义样式的。也就是在 web.xml 文件中配置:

<error-page> 
  <error-code>500</error-code> 
  <location>/error.jsp</location> 
</error-page> 

首先说说自带的逻辑。如果某个 JSP 页面在执行的过程中出现了错误, 那么 JSP 引擎会自动产生一个异常对象,如果这个 JSP 页面指定了另一个 JSP 页面为错误处理程序,那么 JSP 引擎会将这个异常对象放入到 request 对象中,传到错误处理程序中。如果大家有写 Servlet 的印象,这是和那个转向模版 JSP 的 javax.servlet.forward.request_uri 一个思路,保留了原请求的路径而不是 JSP 页面的那个路径。在错误处理程序里,因为 page 编译指令的 isErrorPage 属性的值被设为 true,那么 JSP 引擎会自动声明一个 exception 对象,这个 exception 对象从 request 对象所包含的 HTTP 参数中获得。

request 对象中包含的异常信息非常丰富,如下所示:

你可以用 Java 语句 request.getAttribute("javax.servlet.error.status_code") 获取,也可以在 JSP 页面中通过 EL 表达式来获取,如 ${requestScope["javax.servlet.error.status_code"]}。
这个自定义错误页面虽然简单,JSP 本身也有很好的封装结果,我也看过别人不少的资源,但细究之下也有不少“学问”,于是我想重新再”磨磨这个轮子“——首先 location 是一个 jsp 页面,也可以是 servlet,不过万一 servlet 也有可能启动不起来的话那就使用简单的 JSP 页面就好了。我们通过 JSP 页面定义内部类的方法,达到页面与逻辑的分离(无须编写 servlet)。其余的思路如下:

在 JSP 里面完成 ErrorHandler 类,另有页面调用这个 ErrorHandler 类
不但可以接受 JSP 页面的错误,也可接受 servlet 的控制器传递的错误,并且提取尽量多信息
全部内容先写到内存,然后分别从两个输出流再输出到页面和文件
把错误信息输出到网页的同时,简单加几句话,可以把网页上的信息也写一份到数据库或者文本
可以返回 HTML/JSON/XML
实现代码如下:     

/** 
 * 异常处理类 
*/ 
class ErrorHandler { 
  // 全部内容先写到内存,然后分别从两个输出流再输出到页面和文件 
  private ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
  private PrintStream printStream = new PrintStream(byteArrayOutputStream); 
 
  /** 
   * 收集错误信息 
   * @param request 
   * @param exception 
   * @param out 
   */ 
  public ErrorHandler(HttpServletRequest request, Throwable exception, JspWriter out) { 
    setRequest(request); 
    setException(exception); 
 
    if(out != null) { 
      try { 
        out.print(byteArrayOutputStream); // 输出到网页 
      } catch (IOException e) { 
        e.printStackTrace(); 
      } 
    } 
     
     log(request); 
     
    if(byteArrayOutputStream != null) 
      try { 
        byteArrayOutputStream.close(); 
      } catch (IOException e) { 
        e.printStackTrace(); 
      } 
    if(printStream != null) printStream.close(); 
  } 
 
  /** 
   * 
   * @param request 
   */ 
  private void setRequest(HttpServletRequest request) { 
    printStream.println(); 
    printStream.println("用户账号:" + request.getSession().getAttribute("userName")); 
    printStream.println("访问的路径: "  + getInfo(request, "javax.servlet.forward.request_uri", String.class)); 
    printStream.println("出错页面地址: " + getInfo(request, "javax.servlet.error.request_uri", String.class)); 
    printStream.println("错误代码: "   + getInfo(request, "javax.servlet.error.status_code", int.class)); 
    printStream.println("异常的类型: "  + getInfo(request, "javax.servlet.error.exception_type", Class.class)); 
    printStream.println("异常的信息: "  + getInfo(request, "javax.servlet.error.message", String.class)); 
    printStream.println("异常servlet: " + getInfo(request, "javax.servlet.error.servlet_name", String.class)); 
    printStream.println(); 
     
    // 另外两个对象 
    getInfo(request, "javax.servlet.jspException", Throwable.class); 
    getInfo(request, "javax.servlet.forward.jspException", Throwable.class); 
 
    Map<String, String[]> map = request.getParameterMap(); 
 
    for (String key : map.keySet()) { 
      printStream.println("请求中的 Parameter 包括:"); 
      printStream.println(key + "=" + request.getParameter(key)); 
      printStream.println(); 
    } 
     
    for (Cookie cookie : request.getCookies()){ // cookie.getValue() 
      printStream.println("请求中的 Cookie 包括:"); 
      printStream.println(cookie.getName() + "=" + cookie.getValue()); 
      printStream.println(); 
    } 
 
  } 
 
  /** 
   * 
   * @param exception 
   */ 
  private void setException(Throwable exception) { 
    if (exception != null) { 
      printStream.println("异常信息"); 
      printStream.println(exception.getClass() + " : " + exception.getMessage()); 
      printStream.println(); 
 
      printStream.println("堆栈信息"); 
      exception.printStackTrace(printStream); 
      printStream.println(); 
    } 
  } 
 
    /** 
     * 
     * @param request 
     */ 
    private void log(HttpServletRequest request) { 
      File dir = new File(request.getSession().getServletContext().getRealPath("/errorLog")); 
      if (!dir.exists()) { 
        dir.mkdir(); 
      } 
       
      String timeStamp = new java.text.SimpleDateFormat("yyyyMMddhhmmssS").format(new Date()); 
      File file = new File(dir.getAbsolutePath() + File.separatorChar + "error-" + timeStamp + ".txt"); 
       
//       try(FileOutputStream fileOutputStream = new FileOutputStream(file); 
//         PrintStream ps = new PrintStream(fileOutputStream)){// 写到文件 
//         ps.print(byteArrayOutputStream); 
//       } catch (FileNotFoundException e) { 
//         e.printStackTrace(); 
//       } catch (IOException e) { 
//         e.printStackTrace(); 
//       } catch (Exception e){ 
//         e.printStackTrace(); 
//       } 
    } 
 
    /** 
     * 
     * @param request 
     * @param key 
     * @param type 
     * @return 
     */ 
    @SuppressWarnings("unchecked") 
    private <T> T getInfo(HttpServletRequest request, String key, Class<T> type){ 
      Object obj = request.getAttribute(key); 
      return obj == null ? null : (T) obj; 
    }  
} 

这样就可以完成异常的控制了。下面定义 web.xml,让 tomcat 出错引向我们刚才指定的页面 error.jsp

<!-- 404 页面不存在错误 --> 
<error-page> 
  <error-code>404</error-code> 
  <location>/WEB-INF/jsp/common/default/error.jsp</location> 
</error-page> 
<!-- // --> 
 
<!-- 500 服务器内部错误 --> 
<error-page> 
  <error-code>500</error-code> 
  <location>/WEB-INF/jsp/common/default/error.jsp</location> 
</error-page> 
<!-- // --> 

我们安排一个默认的页面如下

源码如下:

<%@page pageEncoding="UTF-8" isErrorPage="true"%> 
<%@ include file="/WEB-INF/jsp/common/ClassicJSP/util.jsp"%> 
<!DOCTYPE html> 
<html> 
<head> 
  <title>错误页面</title> 
  <style> 
    body { 
      max-width: 600px; 
      min-width: 320px; 
      margin: 0 auto; 
      padding-top: 2%; 
    } 
     
    textarea { 
      width: 100%; 
      min-height: 300px; 
    } 
     
    h1 { 
      text-align: right; 
      color: lightgray; 
    } 
     
    div { 
      margin-top: 1%; 
    } 
  </style> 
</head> 
<body> 
  <h1>抱 歉!</h1> 
  <div style="padding:2% 0;text-indent:2em;">尊敬的用户:我们致力于提供更好的服务,但人算不如天算,有些错误发生了,希望是在控制的范围内……如果问题重复出现,请向系统管理员反馈。</div> 
  <textarea><% 
      new ErrorHandler(request, exception, out); 
      %></textarea> 
  <div> 
    <center> 
      <a href="${pageContext.request.contextPath}">回首页</a> | <a href="javascript:history.go(-1);">上一页</a> 
    </center> 
  </div> 
</body> 
</html> 

以上就是本文的全部内容,希望对大家的学习有所帮助。


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3