在调试servlet程序时,遇到调试窗口右上侧显示RequestFacade和ResponseFacade,具体原因不知,所以在网上查到如下内容:
Tomcat 为什么使用Facade模式对Request对象进行包装?
为了屏蔽内部的catalina容器的相关方法,使用户免受非sevlet标准方法的干扰。
tomcat中request的包装结构:
其中org.apache.coyote.Request是应用层拿到的Request对象的底层实现,不便使用,
tomcat80/Request.java at trunk · apache/tomcat80 · GitHub
/**
*This is a low-level,effiecient representation of a server request. Most
*fields are GC-free, expensive operations are delayed until the user code
*needs the information.
*
*Processing is delegated to modules, using a hook mechanism.
*
*This class is not intended for user code - it is used internally by tomcat
*for processing the request in the most efficient way. Users ( Servlets ) can
*access the infromation using a facade,which provides the high-level view
*of the request.
*
*Tomcat defines a number of attributes:
*<ul>
*<li>"org.apache.tomcat.request" - allow access to the low-level
*request object in trusted applications
*</ul>
*@author James Duncan Davidson [duncan@eng.sun.com]
*@author James Todd [gonzo@eng.sun.com]
*@author Jason Hunter [jch@eng.sun.com]
*@author Harish Prabandham
*@author Alex Cruikshank [alex@epitonic.com]
*@author Hans Bergsten [hans@gefionsoftware.com]
*@author Costin Manolache
*@author Remy Maucherat
*/
org.apache.catalina.connector.Request类(tomcat80/Request.java at trunk · apache/tomcat80 · GitHub)
封装了org.apache.coyote.Request类,实现了HttpServletRequest接口,已经具备了实际
使用能力,不过它还包含了很多Catalina的方法,这些方法不应该暴露给应用层,以免引起与其他
容器实现的兼容性问题。
org.apache.catalina.connector.RequestFacade类(tomcat80/Request.java at trunk · apache/tomcat80 · GitHub)
实现了HttpServletRequest接口,并在其中包含了一个org.apache.catalina.connector.Request对象,
将所有HttpServletRequest接口的调用都代理给org.apache.catalina.connector.Request对象来处理,
这样就屏蔽了Catalina的相关的内部方法,使用户可以专注于servlet的标准方法。
说白了就是除了Servlet标准之外,tomcat自己的request对象内部还有很多成员变量和方法,
但是这些都是不需要暴漏出去的,所以搞了一层包装。