暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Tomcat的错误异常状态返回报告页 Error Report Valve(Valve源码分析之八)

中间件技术讨论圈 2017-02-20
1796

对于一般的服务器来说,如果出现错误的话,如果不处理的话,其实页面返回就是一个空白页面;


对于高手来说,你可以通过response的响应码,自己找一些firebug这种浏览器的插件去调试network返回值;


但是这种做法是不宜用的,对于应用服务器来说,一般都会在出现异常的时候,做一点什么;


如Tomcat,当出现一些异常状况的时候,如404界面,就会返回一个html:

type:返回error报告的类型

message:error的信息

description:这个error的解释

这个页面是怎么形成的呢?


这个就是Error Report Valve:

Error Report Valve

Introduction

The Error Report Valve is a simple error handler for HTTP status codes that will generate and return HTML error pages.

NOTE: Disabling both showServerInfo and showReport will only return the HTTP status code and remove all CSS.

从解释上来看,Error Report Valve是一个专门处理http状态错误码返回的页面handler,它的作用是生成固定格式的html页面,能让访问者非常清晰的看明白我这个访问出现的异常状态是什么;


从源码上来分析这个Error Report Valve,很简单,先看invoke方法:

通过invoke的pipeline的链条,当整个链条出现错误的时候,可以从

request.getAttribute(RequestDispatcher.ERROR_EXCEPTION);  

中获取到error异常,代表整个请求链条出现异常了;

那么,如果配置了该Error Report Valve的话,那么会调用report进行攒出来一个error的html页面,就是如本文最上面的那张图的html;

我们来看看report方法:

其实report没什么可说的,就是输出html,拼接字符串。

这里面有几个属性:

Attributes

The Error Report Valve supports the following configuration attributes:

AttributeDescription
className

Java class name of the implementation to use. This MUST be set to org.apache.catalina.valves.ErrorReportValve to use the default error report valve.


你可以自定义 ErrorReportValve 实现你自己的逻辑;

showReport

Flag to determine if the error report is presented when an error occurs. If set to false
, then the error report is not in the HTML response. Default value: true



是否在errorhtml中加上
这部分

 

showServerInfo

Flag to determine if server information is presented when an error occurs. If set to false
, then the server version is not returned in the HTML response. Default value: true



是否在errorhtml中加上
这部分

 

对于Error Report Valve你可以进行自定义到任何的级别当中,基于你自定义的error返回页面的html,你可以自己设计,这样的话,也需要修改classname属性;


当然,对于Host这一级,其实默认有一个属性,errorReportValveClass属性

 

 默认就是指向这个org.apache.catalina.valves.ErrorReportValve
 这个Tomcat的默认Error Report Valve实现;

这也就是为什么你啥都不用配置,Tomcat就给你返回这个error页面的原因;


文章转载自中间件技术讨论圈,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论