1. /**
  2. *
  3. * Provides a convenient implementation of the ServletRequest interface that
  4. * can be subclassed by developers wishing to adapt the request to a Servlet.
  5. * This class implements the Wrapper or Decorator pattern. Methods default to
  6. * calling through to the wrapped request object.
  7. * @since v 2.3
  8. *
  9. *
  10. *
  11. * @see javax.servlet.ServletRequest
  12. *
  13. */
  14. //实现了ServletRequest接口
  15. public class ServletRequestWrapper implements ServletRequest {
  16. private ServletRequest request;
  17.  
  18. /**
  19. * Creates a ServletRequest adaptor wrapping the given request object.
  20. * @throws java.lang.IllegalArgumentException if the request is null
  21. */
  22. //构造函数,ServletRequest适配器
  23. public ServletRequestWrapper(ServletRequest request) {
  24. if (request == null) {
  25. throw new IllegalArgumentException("Request cannot be null");
  26. }
  27. this.request = request;
  28. }
  29.  
  30. /**
  31. * Return the wrapped request object.
  32. */
  33. //获得Request
  34. public ServletRequest getRequest() {
  35. return this.request;
  36. }
  37. /**
  38. * Sets the request object being wrapped.
  39. * @throws java.lang.IllegalArgumentException if the request is null.
  40. */
  41. //设置ServletRequest
  42. public void setRequest(ServletRequest request) {
  43. if (request == null) {
  44. throw new IllegalArgumentException("Request cannot be null");
  45. }
  46. this.request = request;
  47. }
  48. /**
  49. * The default behavior of this method is to call getAttribute(String name)
  50. * on the wrapped request object.
  51. */
  52. //获取ServletRequest的name属性
  53. public Object getAttribute(String name) {
  54. return this.request.getAttribute(name);
  55. }
  56. /**
  57. * The default behavior of this method is to return getAttributeNames()
  58. * on the wrapped request object.
  59. */
  60. //获得所有属性name的枚举
  61. public Enumeration getAttributeNames() {
  62. return this.request.getAttributeNames();
  63. }
  64. /**
  65. * The default behavior of this method is to return getCharacterEncoding()
  66. * on the wrapped request object.
  67. */
  68. //获得字符类型
  69. public String getCharacterEncoding() {
  70. return this.request.getCharacterEncoding();
  71. }
  72. /**
  73. * The default behavior of this method is to set the character encoding
  74. * on the wrapped request object.
  75. */
  76. //设置字符类型
  77. public void setCharacterEncoding(String enc) throws java.io.UnsupportedEncodingException {
  78. this.request.setCharacterEncoding(enc);
  79. }
  80. /**
  81. * The default behavior of this method is to return getContentLength()
  82. * on the wrapped request object.
  83. */
  84. //获得Request请求body的二进制数据的长度
  85. public int getContentLength() {
  86. return this.request.getContentLength();
  87. }
  88. /**
  89. * The default behavior of this method is to return getContentType()
  90. * on the wrapped request object.
  91. */
  92. //获得Request请求body的文件类型
  93. public String getContentType() {
  94. return this.request.getContentType();
  95. }
  96. /**
  97. * The default behavior of this method is to return getInputStream()
  98. * on the wrapped request object.
  99. */
  100. //获得InputStream
  101. public ServletInputStream getInputStream() throws IOException {
  102. return this.request.getInputStream();
  103. }
  104. /**
  105. * The default behavior of this method is to return getParameter(String name)
  106. * on the wrapped request object.
  107. */
  108. //获得参数name
  109. public String getParameter(String name) {
  110. return this.request.getParameter(name);
  111. }
  112. /**
  113. * The default behavior of this method is to return getParameterMap()
  114. * on the wrapped request object.
  115. */
  116. public Map getParameterMap() {
  117. return this.request.getParameterMap();
  118. }
  119. /**
  120. * The default behavior of this method is to return getParameterNames()
  121. * on the wrapped request object.
  122. */
  123. //获得属性name的枚举
  124. public Enumeration getParameterNames() {
  125. return this.request.getParameterNames();
  126. }
  127. /**
  128. * The default behavior of this method is to return getParameterValues(String name)
  129. * on the wrapped request object.
  130. */
  131. //获得属性name的所有值
  132. public String[] getParameterValues(String name) {
  133. return this.request.getParameterValues(name);
  134. }
  135. /**
  136. * The default behavior of this method is to return getProtocol()
  137. * on the wrapped request object.
  138. */
  139. //获取协议
  140. public String getProtocol() {
  141. return this.request.getProtocol();
  142. }
  143. /**
  144. * The default behavior of this method is to return getScheme()
  145. * on the wrapped request object.
  146. */
  147. //返回协议名称 http、https、ftp
  148. public String getScheme() {
  149. return this.request.getScheme();
  150. }
  151. /**
  152. * The default behavior of this method is to return getServerName()
  153. * on the wrapped request object.
  154. */
  155. //获得请求发送的服务器名称
  156. public String getServerName() {
  157. return this.request.getServerName();
  158. }
  159. /**
  160. * The default behavior of this method is to return getServerPort()
  161. * on the wrapped request object.
  162. */
  163. //获得请求发送的端口
  164. public int getServerPort() {
  165. return this.request.getServerPort();
  166. }
  167.  
  168. /**
  169. * The default behavior of this method is to return getReader()
  170. * on the wrapped request object.
  171. */
  172. //检索请求body数据作为字符数据使用bufferedReader
  173. public BufferedReader getReader() throws IOException {
  174. return this.request.getReader();
  175. }
  176. /**
  177. * The default behavior of this method is to return getRemoteAddr()
  178. * on the wrapped request object.
  179. */
  180. //获得远程主机IP
  181. public String getRemoteAddr() {
  182. return this.request.getRemoteAddr();
  183. }
  184. /**
  185. * The default behavior of this method is to return getRemoteHost()
  186. * on the wrapped request object.
  187. */
  188. //获得发送请求的主机名或最后一次代理的主机名
  189. public String getRemoteHost() {
  190. return this.request.getRemoteHost();
  191. }
  192. /**
  193. * The default behavior of this method is to return setAttribute(String name, Object o)
  194. * on the wrapped request object.
  195. */
  196. //在请求中设置一个属性
  197. public void setAttribute(String name, Object o) {
  198. this.request.setAttribute(name, o);
  199. }
  200. /**
  201. * The default behavior of this method is to call removeAttribute(String name)
  202. * on the wrapped request object.
  203. */
  204. //删除请求的属性name
  205. public void removeAttribute(String name) {
  206. this.request.removeAttribute(name);
  207. }
  208. /**
  209. * The default behavior of this method is to return getLocale()
  210. * on the wrapped request object.
  211. */
  212.  
  213. public Locale getLocale() {
  214. return this.request.getLocale();
  215. }
  216. /**
  217. * The default behavior of this method is to return getLocales()
  218. * on the wrapped request object.
  219. */
  220. public Enumeration getLocales() {
  221. return this.request.getLocales();
  222. }
  223. /**
  224. * The default behavior of this method is to return isSecure()
  225. * on the wrapped request object.
  226. */
  227. //判断是否使用的安全协议https
  228. public boolean isSecure() {
  229. return this.request.isSecure();
  230. }
  231. /**
  232. * The default behavior of this method is to return getRequestDispatcher(String path)
  233. * on the wrapped request object.
  234. */
  235. //获得path的分发器
  236. public RequestDispatcher getRequestDispatcher(String path) {
  237. return this.request.getRequestDispatcher(path);
  238. }
  239. /**
  240. * The default behavior of this method is to return getRealPath(String path)
  241. * on the wrapped request object.
  242. */
  243.  
  244. public String getRealPath(String path) {
  245. return this.request.getRealPath(path);
  246. }
  247.  
  248. /**
  249. * The default behavior of this method is to return
  250. * getRemotePort() on the wrapped request object.
  251. *
  252. * @since 2.4
  253. */
  254. //端口
  255. public int getRemotePort(){
  256. return this.request.getRemotePort();
  257. }
  258. /**
  259. * The default behavior of this method is to return
  260. * getLocalName() on the wrapped request object.
  261. *
  262. * @since 2.4
  263. */
  264. //获得Request的服务器名
  265. public String getLocalName(){
  266. return this.request.getLocalName();
  267. }
  268.  
  269. /**
  270. * The default behavior of this method is to return
  271. * getLocalAddr() on the wrapped request object.
  272. *
  273. * @since 2.4
  274. */
  275. //获得Request的服务器IP地址
  276. public String getLocalAddr(){
  277. return this.request.getLocalAddr();
  278. }
  279. /**
  280. * The default behavior of this method is to return
  281. * getLocalPort() on the wrapped request object.
  282. *
  283. * @since 2.4
  284. */
  285. //端口
  286. public int getLocalPort(){
  287. return this.request.getLocalPort();
  288. }
  289.  
  290. }

Java-ServletRequestWrapper的更多相关文章

  1. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  2. java.lang.ClassCastException: $Proxy0 cannot be cast to javax.servlet.ServletRequestWrapper

    我是在myeclipse 8.5使用代理的时候遇到这个问题的.因为我是跟着视频敲的,所以按道理代码是不会有问题..最后经过我百折不挠的折腾,我发现问题的根源 是 myeclipse自带的tomcat. ...

  3. java web学习总结(十八) -------------------过滤器的高级使用

    在filter中可以得到代表用户请求和响应的request.response对象,因此在编程中可以使用Decorator(装饰器)模式对request.response对象进行包装,再把包装对象传给目 ...

  4. Java程序员学习之路

    1. Java语言基础 谈到Java语 言基础学习的书籍,大家肯定会推荐Bruce Eckel的<Thinking in Java>.它是一本写的相当深刻的技术书籍,Java语言基础部分基 ...

  5. [javaweb]Java过滤器与包装设计模式的实用案例.

    在filter中可以得到代表用户请求和响应的request.response对象,因此在编程中可以使用Decorator(装饰器)模式对request.response对象进行包装,再把包装对象传给目 ...

  6. 【转】学习JAVA的步骤

      好东西大家分享: JAVA学习的一些重点 . Java语言基础 谈到Java语言基础学习的书籍,大家肯定会推荐Bruce Eckel的<Thinking in Java>.它是一本写的 ...

  7. java web servlet

    一.什么是Servlet Servlet是一种小型的Java程序,它扩展了Web服务器的功能.作为一种服务器端的应用,他是运行在Servlet容器当中,例如Tomcat就是一种流行的Servlet容器 ...

  8. java专业规划(转载)

    1. Java语言基础     谈到Java语言基础学习的书籍,大家肯定会推荐Bruce Eckel的<Thinking in Java>.它是一本写的相当深刻的技术书籍,Java语言基础 ...

  9. 【转】安卓必备Java基础

    [转]http://www.zhihu.com/question/19937886(里面提到的知识点的链接) 1. Java 语言基础 谈到Java 语言基础学习的书籍,大家肯定会推荐Bruce Ec ...

  10. java学习书籍推荐

    1. Java 语言基础 谈到Java 语言基础学习的书籍,大家肯定会推荐Bruce Eckel 的<Thinking in Java >.它是一本写的相当深刻的技术书籍,Java 语言基 ...

随机推荐

  1. Matlab to OpenCV Mat

    convert Matlab matrix to OpenCV Mat. Support CV_32FC3 only currently. The Code int matlab2opencv(cv: ...

  2. Why Doesn't Python Have Switch/Case?

    Why Doesn't Python Have Switch/Case? Tuesday, June 09, 2015 (permalink) Unlike every other programmi ...

  3. Android初级教程:如何自定义一个状态选择器

    有这样一种场景:点击一下某个按钮或者图片(view),改变了样式(一般改变背景颜色).这个时候一种解决方案,可能就是状态选择器.接下来就介绍如何实现状态选择器: 步骤: 一.新建这样的文件夹:res/ ...

  4. 指令汇B新闻客户端开发(五) ShareSdk的使用

    ShareSdk是一个分享按钮的开源框架,我们首先可以去mob的官网下载这个控件.mob官网,然后找到sdk下载那一栏, 下载下来之后点击这个.jar文件就会有一个弹窗,填写自己的应用包名和要哪些分享 ...

  5. Oracle 执行计划(Explain Plan) 说明

    如果要分析某条SQL的性能问题,通常我们要先看SQL的执行计划,看看SQL的每一步执行是否存在问题. 如果一条SQL平时执行的好好的,却有一天突然性能很差,如果排除了系统资源和阻塞的原因,那么基本可以 ...

  6. Matlab:如何查找给定目录下的文件

    我们有很多目录,每个目录下都有些有用的文件,比如图像文件,如何自动的扫描这些文件呢? 可以使用dir函数来完成这个任务. 比如假设给定目录 baseDir,它是一个字符串,包含的是某个目录,例如'./ ...

  7. Python读取JSON数据,并解决字符集不匹配问题

    今天来谈一谈Python解析JSON数据,并写入到本地文件的一个小例子. – 思路如下 从一个返回JSON天气数据的网站获取到目标JSON数据串 使用Python解析出需要的部分 写入到本地文件,供其 ...

  8. 13 SQLiteOpenHelper SQLiteDatabase详解

    创建数据库: 1. 创建一个类继承SQLiteOpenHelper 2. 创建继承对象 new SQLiteOpenHelper() 3. 用创建的对象获取可写或者可读的SQLiteDatabase ...

  9. String&StringBuilder&StringBuffer总结

    一.String的JVM内存分配测试与分析           String a="a"; String b="b"; String c="ab&qu ...

  10. Git工程迁移方法总结(命令行)

    Git工程迁移方法总结 Git工程迁移方法总结 Git最近准备迁移一下位置,这里采用命令行的方式,做如下操作. 1.git init  初始化git仓库,这个时候发现本地文件夹多了个.git的文件夹. ...