欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-09 16:30:42
发生服务器 500 异常,如果默认方式处理,则是将异常捕获之后跳到 tomcat 缺省的异常页面,如下图所示。 不论哪个网站都是一样的,所以为了满足自定义的需要,to...

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

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

不论哪个网站都是一样的,所以为了满足自定义的需要,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 Web错误/异常处理页面

你可以用 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> 
<!-- // --> 

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

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

源码如下:

<%@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> 

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