1. /**
  2. * Defines an object to assist a servlet in sending a response to the client.
  3. * The servlet container creates a <code>ServletResponse</code> object and
  4. * passes it as an argument to the servlet's <code>service</code> method.
  5. *
  6. * <p>To send binary data in a MIME body response, use
  7. * the {@link ServletOutputStream} returned by {@link #getOutputStream}.
  8. * To send character data, use the <code>PrintWriter</code> object
  9. * returned by {@link #getWriter}. To mix binary and text data,
  10. * for example, to create a multipart response, use a
  11. * <code>ServletOutputStream</code> and manage the character sections
  12. * manually.
  13. *
  14. * <p>The charset for the MIME body response can be specified
  15. * explicitly using the {@link #setCharacterEncoding} and
  16. * {@link #setContentType} methods, or implicitly
  17. * using the {@link #setLocale} method.
  18. * Explicit specifications take precedence over
  19. * implicit specifications. If no charset is specified, ISO-8859-1 will be
  20. * used. The <code>setCharacterEncoding</code>,
  21. * <code>setContentType</code>, or <code>setLocale</code> method must
  22. * be called before <code>getWriter</code> and before committing
  23. * the response for the character encoding to be used.
  24. * <p>See the Internet RFCs such as
  25. * <a href="http://www.ietf.org/rfc/rfc2045.txt">
  26. * RFC 2045</a> for more information on MIME. Protocols such as SMTP
  27. * and HTTP define profiles of MIME, and those standards
  28. * are still evolving.
  29. * @author Various
  30. * @version $Version$
  31. *
  32. * @see ServletOutputStream
  33. *
  34. */
  35. public interface ServletResponse {
  36. /**
  37. * Returns the name of the character encoding (MIME charset)
  38. * used for the body sent in this response.
  39. * The character encoding may have been specified explicitly
  40. * using the {@link #setCharacterEncoding} or
  41. * {@link #setContentType} methods, or implicitly using the
  42. * {@link #setLocale} method. Explicit specifications take
  43. * precedence over implicit specifications. Calls made
  44. * to these methods after <code>getWriter</code> has been
  45. * called or after the response has been committed have no
  46. * effect on the character encoding. If no character encoding
  47. * has been specified, <code>ISO-8859-1</code> is returned.
  48. * <p>See RFC 2047 (http://www.ietf.org/rfc/rfc2047.txt)
  49. * for more information about character encoding and MIME.
  50. * @return a <code>String</code> specifying the
  51. * name of the character encoding, for
  52. * example, <code>UTF-8</code>
  53. */
  54. //获得返回body的字符类型
  55. public String getCharacterEncoding();
  56. /**
  57. * Returns the content type used for the MIME body
  58. * sent in this response. The content type proper must
  59. * have been specified using {@link #setContentType}
  60. * before the response is committed. If no content type
  61. * has been specified, this method returns null.
  62. * If a content type has been specified and a
  63. * character encoding has been explicitly or implicitly
  64. * specified as described in {@link #getCharacterEncoding},
  65. * the charset parameter is included in the string returned.
  66. * If no character encoding has been specified, the
  67. * charset parameter is omitted.
  68. *
  69. * @return a <code>String</code> specifying the
  70. * content type, for example,
  71. * <code>text/html; charset=UTF-8</code>,
  72. * or null
  73. *
  74. * @since 2.4
  75. */
  76. //body中的文件类型
  77. public String getContentType();
  78. /**
  79. * Returns a {@link ServletOutputStream} suitable for writing binary
  80. * data in the response. The servlet container does not encode the
  81. * binary data.
  82. * <p> Calling flush() on the ServletOutputStream commits the response.
  83. * Either this method or {@link #getWriter} may
  84. * be called to write the body, not both.
  85. * @return a {@link ServletOutputStream} for writing binary data
  86. * @exception IllegalStateException if the <code>getWriter</code> method has been called on this response
  87. * @exception IOException if an input or output exception occurred
  88. * @see #getWriter
  89. */
  90. //
  91. public ServletOutputStream getOutputStream() throws IOException;
  92. /**
  93. * Returns a <code>PrintWriter</code> object that
  94. * can send character text to the client.
  95. * The <code>PrintWriter</code> uses the character
  96. * encoding returned by {@link #getCharacterEncoding}.
  97. * If the response's character encoding has not been
  98. * specified as described in <code>getCharacterEncoding</code>
  99. * (i.e., the method just returns the default value
  100. * <code>ISO-8859-1</code>), <code>getWriter</code>
  101. * updates it to <code>ISO-8859-1</code>.
  102. * <p>Calling flush() on the <code>PrintWriter</code>
  103. * commits the response.
  104. * <p>Either this method or {@link #getOutputStream} may be called
  105. * to write the body, not both.
  106. * @return a <code>PrintWriter</code> object that
  107. * can return character data to the client
  108. * @exception UnsupportedEncodingException
  109. * if the character encoding returned
  110. * by <code>getCharacterEncoding</code> cannot be used
  111. * @exception IllegalStateException
  112. * if the <code>getOutputStream</code>
  113. * method has already been called for this
  114. * response object
  115. * @exception IOException
  116. * if an input or output exception occurred
  117. * @see #getOutputStream
  118. * @see #setCharacterEncoding
  119. *
  120. */
  121. //
  122. public PrintWriter getWriter() throws IOException;
  123. /**
  124. * Sets the character encoding (MIME charset) of the response
  125. * being sent to the client, for example, to UTF-8.
  126. * If the character encoding has already been set by
  127. * {@link #setContentType} or {@link #setLocale},
  128. * this method overrides it.
  129. * Calling {@link #setContentType} with the <code>String</code>
  130. * of <code>text/html</code> and calling
  131. * this method with the <code>String</code> of <code>UTF-8</code>
  132. * is equivalent with calling
  133. * <code>setContentType</code> with the <code>String</code> of
  134. * <code>text/html; charset=UTF-8</code>.
  135. * <p>This method can be called repeatedly to change the character
  136. * encoding.
  137. * This method has no effect if it is called after
  138. * <code>getWriter</code> has been
  139. * called or after the response has been committed.
  140. * <p>Containers must communicate the character encoding used for
  141. * the servlet response's writer to the client if the protocol
  142. * provides a way for doing so. In the case of HTTP, the character
  143. * encoding is communicated as part of the <code>Content-Type</code>
  144. * header for text media types. Note that the character encoding
  145. * cannot be communicated via HTTP headers if the servlet does not
  146. * specify a content type; however, it is still used to encode text
  147. * written via the servlet response's writer.
  148. * @param charset a String specifying only the character set
  149. * defined by IANA Character Sets
  150. * (http://www.iana.org/assignments/character-sets)
  151. * @see #setContentType
  152. * #setLocale
  153. * @since 2.4
  154. *
  155. */
  156. //设置字符类型
  157. public void setCharacterEncoding(String charset);
  158. /**
  159. * Sets the length of the content body in the response
  160. * In HTTP servlets, this method sets the HTTP Content-Length header.
  161. * @param len an integer specifying the length of the
  162. * content being returned to the client; sets the Content-Length header
  163. */
  164. //设置response body中内容的长度
  165. public void setContentLength(int len);
  166. /**
  167. * Sets the content type of the response being sent to
  168. * the client, if the response has not been committed yet.
  169. * The given content type may include a character encoding
  170. * specification, for example, <code>text/html;charset=UTF-8</code>.
  171. * The response's character encoding is only set from the given
  172. * content type if this method is called before <code>getWriter</code>
  173. * is called.
  174. * <p>This method may be called repeatedly to change content type and
  175. * character encoding.
  176. * This method has no effect if called after the response
  177. * has been committed. It does not set the response's character
  178. * encoding if it is called after <code>getWriter</code>
  179. * has been called or after the response has been committed.
  180. * <p>Containers must communicate the content type and the character
  181. * encoding used for the servlet response's writer to the client if
  182. * the protocol provides a way for doing so. In the case of HTTP,
  183. * the <code>Content-Type</code> header is used.
  184. * @param type a <code>String</code> specifying the MIME
  185. * type of the content
  186. * @see #setLocale
  187. * @see #setCharacterEncoding
  188. * @see #getOutputStream
  189. * @see #getWriter
  190. *
  191. */
  192. //返回给客户端的内容类型,text/html;charset=UTF-8
  193. public void setContentType(String type);
  194. /**
  195. * Sets the preferred buffer size for the body of the response.
  196. * The servlet container will use a buffer at least as large as
  197. * the size requested. The actual buffer size used can be found
  198. * using <code>getBufferSize</code>.
  199. * <p>A larger buffer allows more content to be written before anything is
  200. * actually sent, thus providing the servlet with more time to set
  201. * appropriate status codes and headers. A smaller buffer decreases
  202. * server memory load and allows the client to start receiving data more
  203. * quickly.
  204. * <p>This method must be called before any response body content is
  205. * written; if content has been written or the response object has
  206. * been committed, this method throws an
  207. * <code>IllegalStateException</code>.
  208. * @param size the preferred buffer size
  209. * @exception IllegalStateException if this method is called after
  210. * content has been written
  211. * @see #getBufferSize
  212. * @see #flushBuffer
  213. * @see #isCommitted
  214. * @see #reset
  215. *
  216. */
  217. //response body缓冲区大小
  218. public void setBufferSize(int size);
  219. /**
  220. * Returns the actual buffer size used for the response. If no buffering
  221. * is used, this method returns 0.
  222. * @return the actual buffer size used
  223. * @see #setBufferSize
  224. * @see #flushBuffer
  225. * @see #isCommitted
  226. * @see #reset
  227. */
  228. //获得response使用的真是的缓冲区大小
  229. public int getBufferSize();
  230. /**
  231. * Forces any content in the buffer to be written to the client. A call
  232. * to this method automatically commits the response, meaning the status
  233. * code and headers will be written.
  234. * @see #setBufferSize
  235. * @see #getBufferSize
  236. * @see #isCommitted
  237. * @see #reset
  238. */
  239. //刷新缓冲区内容到客户端
  240. public void flushBuffer() throws IOException;
  241. /**
  242. * Clears the content of the underlying buffer in the response without
  243. * clearing headers or status code. If the
  244. * response has been committed, this method throws an
  245. * <code>IllegalStateException</code>.
  246. * @see #setBufferSize
  247. * @see #getBufferSize
  248. * @see #isCommitted
  249. * @see #reset
  250. *
  251. * @since 2.3
  252. */
  253. //
  254. public void resetBuffer();
  255. /**
  256. * Returns a boolean indicating if the response has been
  257. * committed. A committed response has already had its status
  258. * code and headers written.
  259. * @return a boolean indicating if the response has been
  260. * committed
  261. * @see #setBufferSize
  262. * @see #getBufferSize
  263. * @see #flushBuffer
  264. * @see #reset
  265. *
  266. */
  267. //
  268. public boolean isCommitted();
  269. /**
  270. * Clears any data that exists in the buffer as well as the status code and
  271. * headers. If the response has been committed, this method throws an
  272. * <code>IllegalStateException</code>.
  273. *
  274. * @exception IllegalStateException if the response has already been
  275. * committed
  276. * @see #setBufferSize
  277. * @see #getBufferSize
  278. * @see #flushBuffer
  279. * @see #isCommitted
  280. */
  281.  
  282. public void reset();
  283. /**
  284. * Sets the locale of the response, if the response has not been
  285. * committed yet. It also sets the response's character encoding
  286. * appropriately for the locale, if the character encoding has not
  287. * been explicitly set using {@link #setContentType} or
  288. * {@link #setCharacterEncoding}, <code>getWriter</code> hasn't
  289. * been called yet, and the response hasn't been committed yet.
  290. * If the deployment descriptor contains a
  291. * <code>locale-encoding-mapping-list</code> element, and that
  292. * element provides a mapping for the given locale, that mapping
  293. * is used. Otherwise, the mapping from locale to character
  294. * encoding is container dependent.
  295. * <p>This method may be called repeatedly to change locale and
  296. * character encoding. The method has no effect if called after the
  297. * response has been committed. It does not set the response's
  298. * character encoding if it is called after {@link #setContentType}
  299. * has been called with a charset specification, after
  300. * {@link #setCharacterEncoding} has been called, after
  301. * <code>getWriter</code> has been called, or after the response
  302. * has been committed.
  303. * <p>Containers must communicate the locale and the character encoding
  304. * used for the servlet response's writer to the client if the protocol
  305. * provides a way for doing so. In the case of HTTP, the locale is
  306. * communicated via the <code>Content-Language</code> header,
  307. * the character encoding as part of the <code>Content-Type</code>
  308. * header for text media types. Note that the character encoding
  309. * cannot be communicated via HTTP headers if the servlet does not
  310. * specify a content type; however, it is still used to encode text
  311. * written via the servlet response's writer.
  312. * @param loc the locale of the response
  313. * @see #getLocale
  314. * @see #setContentType
  315. * @see #setCharacterEncoding
  316. */
  317. public void setLocale(Locale loc);
  318. /**
  319. * Returns the locale specified for this response
  320. * using the {@link #setLocale} method. Calls made to
  321. * <code>setLocale</code> after the response is committed
  322. * have no effect. If no locale has been specified,
  323. * the container's default locale is returned.
  324. * @see #setLocale
  325. */
  326. public Locale getLocale();
  327. }
  1. /**
  2. *
  3. * Provides a convenient implementation of the ServletResponse interface that
  4. * can be subclassed by developers wishing to adapt the response from a Servlet.
  5. * This class implements the Wrapper or Decorator pattern. Methods default to
  6. * calling through to the wrapped response object.
  7. *
  8. * @author Various
  9. * @version $Version$
  10. * @since v 2.3
  11. *
  12. * @see javax.servlet.ServletResponse
  13. *
  14. */
  15.  
  16. public class ServletResponseWrapper implements ServletResponse {
  17. private ServletResponse response;
  18. /**
  19. * Creates a ServletResponse adaptor wrapping the given response object.
  20. * @throws java.lang.IllegalArgumentException if the response is null.
  21. */
  22.  
  23. public ServletResponseWrapper(ServletResponse response) {
  24. if (response == null) {
  25. throw new IllegalArgumentException("Response cannot be null");
  26. }
  27. this.response = response;
  28. }
  29.  
  30. /**
  31. * Return the wrapped ServletResponse object.
  32. */
  33.  
  34. public ServletResponse getResponse() {
  35. return this.response;
  36. }
  37.  
  38. /**
  39. * Sets the response being wrapped.
  40. * @throws java.lang.IllegalArgumentException if the response is null.
  41. */
  42.  
  43. public void setResponse(ServletResponse response) {
  44. if (response == null) {
  45. throw new IllegalArgumentException("Response cannot be null");
  46. }
  47. this.response = response;
  48. }
  49.  
  50. /**
  51. * The default behavior of this method is to call setCharacterEncoding(String charset)
  52. * on the wrapped response object.
  53. *
  54. * @since 2.4
  55. */
  56.  
  57. public void setCharacterEncoding(String charset) {
  58. this.response.setCharacterEncoding(charset);
  59. }
  60.  
  61. /**
  62. * The default behavior of this method is to return getCharacterEncoding()
  63. * on the wrapped response object.
  64. */
  65.  
  66. public String getCharacterEncoding() {
  67. return this.response.getCharacterEncoding();
  68. }
  69.  
  70. /**
  71. * The default behavior of this method is to return getOutputStream()
  72. * on the wrapped response object.
  73. */
  74.  
  75. public ServletOutputStream getOutputStream() throws IOException {
  76. return this.response.getOutputStream();
  77. }
  78.  
  79. /**
  80. * The default behavior of this method is to return getWriter()
  81. * on the wrapped response object.
  82. */
  83.  
  84. public PrintWriter getWriter() throws IOException {
  85. return this.response.getWriter();
  86. }
  87.  
  88. /**
  89. * The default behavior of this method is to call setContentLength(int len)
  90. * on the wrapped response object.
  91. */
  92.  
  93. public void setContentLength(int len) {
  94. this.response.setContentLength(len);
  95. }
  96.  
  97. /**
  98. * The default behavior of this method is to call setContentType(String type)
  99. * on the wrapped response object.
  100. */
  101.  
  102. public void setContentType(String type) {
  103. this.response.setContentType(type);
  104. }
  105.  
  106. /**
  107. * The default behavior of this method is to return getContentType()
  108. * on the wrapped response object.
  109. *
  110. * @since 2.4
  111. */
  112.  
  113. public String getContentType() {
  114. return this.response.getContentType();
  115. }
  116.  
  117. /**
  118. * The default behavior of this method is to call setBufferSize(int size)
  119. * on the wrapped response object.
  120. */
  121. public void setBufferSize(int size) {
  122. this.response.setBufferSize(size);
  123. }
  124.  
  125. /**
  126. * The default behavior of this method is to return getBufferSize()
  127. * on the wrapped response object.
  128. */
  129. public int getBufferSize() {
  130. return this.response.getBufferSize();
  131. }
  132.  
  133. /**
  134. * The default behavior of this method is to call flushBuffer()
  135. * on the wrapped response object.
  136. */
  137.  
  138. public void flushBuffer() throws IOException {
  139. this.response.flushBuffer();
  140. }
  141.  
  142. /**
  143. * The default behavior of this method is to return isCommitted()
  144. * on the wrapped response object.
  145. */
  146. public boolean isCommitted() {
  147. return this.response.isCommitted();
  148. }
  149.  
  150. /**
  151. * The default behavior of this method is to call reset()
  152. * on the wrapped response object.
  153. */
  154.  
  155. public void reset() {
  156. this.response.reset();
  157. }
  158.  
  159. /**
  160. * The default behavior of this method is to call resetBuffer()
  161. * on the wrapped response object.
  162. */
  163.  
  164. public void resetBuffer() {
  165. this.response.resetBuffer();
  166. }
  167.  
  168. /**
  169. * The default behavior of this method is to call setLocale(Locale loc)
  170. * on the wrapped response object.
  171. */
  172.  
  173. public void setLocale(Locale loc) {
  174. this.response.setLocale(loc);
  175. }
  176.  
  177. /**
  178. * The default behavior of this method is to return getLocale()
  179. * on the wrapped response object.
  180. */
  181. public Locale getLocale() {
  182. return this.response.getLocale();
  183. }
  184.  
  185. }

Java-ServletResponse-ServletResponseWrapper的更多相关文章

  1. Spark案例分析

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

  2. Java Servlet(四):Servlet接口service工作(ServletRequest,ServletResponse对象)(jdk7+tomcat7+eclipse)

    本篇将会记录,Servlet接收客户端传递来的参数信息,并返回信息使用的对象,及这些对象的函数相关用法. 还是在java ee工程中进行操作,在WebContent目录下创建一个login.jsp文件 ...

  3. [原创]java WEB学习笔记09:ServletResponse & HttpServletResponse

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  4. Java EE javax.servlet中的ServletResponse接口

    ServletResponse接口 public interface ServletResponse 子接口:HttpServletResponse 实现类:HttpServletResponseWr ...

  5. java web servlet

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

  6. java HttpServletRequest和HttpServletResponse詳解

    這篇文章主要介紹瞭java HttpServletRequest和HttpServletResponse詳解的相關資料,需要的朋友可以參考下 java HttpServletRequest和HttpS ...

  7. Java精选笔记_Servlet技术

    Servlet技术 Servlet开发入门 Servlet接口 针对Servlet技术的开发,SUN公司提供了一系列接口和类,其中最重要的是javax.servlet.Servlet接口. Servl ...

  8. 《Tomcat与Java Web开发技术详解》思维导图

    越想构建上层建筑,就越觉得底层基础很重要.补课系列. 书是良心书,就是太基础了,正适合补课. [纯文字版] Tomcat与Java Web开发技术详解 Servlet Servlet的生命周期 初始化 ...

  9. [原创]java WEB学习笔记15:域对象的属性操作(pageContext,request,session,application) 及 请求的重定向和转发

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  10. Java HttpServletRequest中getAttribute()方法和getParameter()区别

    一.ServletRequest接口 HttpServletRequest接口继承了ServletRequest接口,实现类通常代表一个实际的Http Request. Servlet容器负责创建一个 ...

随机推荐

  1. Swift3中如何为Array写一个限定Type的扩展

    我们知道Swift可以扩展已存在的类或结构,这些类或结构可以存在于标准库(或称为核心库)中.如果结构是一个集合类型(比如Array)就更有趣了.我们想尝试写一个限定Type数组的扩展,So我们就拿Ar ...

  2. 如何使用Matlab产生对称矩阵

    有时候做实验需要使用对称矩阵,这里介绍如何使用Matlab产生随机的对称矩阵. 用例子说明一下:我要产生4X4的随机矩阵,要求是对称矩阵. 产生对称矩阵 A = rand(4); B = tril(A ...

  3. MySQL 视图技术

    以前也只是知道数据库中有视图这么个概念,但是没有去深究,今天正好有时间,就来总结一下吧. 视图的定义 视图就是从一个或多个表中,导出来的表,是一个虚拟存在的表.视图就像一个窗口(数据展示的窗口),通过 ...

  4. 高通msm8994性能及温度监测脚本

    [plain] view plain copystartTime=$(date +%Y-%m-%d-%H-%M-%S)  pathName="/data/cpu_logs"  fi ...

  5. shell 数据流重定向操作符总结

    最近看了鸟哥私房菜关于shell数据流重定向的内容,总结一下. 操作符: 1.标准输入(stdin):代码为0,符号:< 或者<< 2.标准输出(stdout):代码为1,符号:&g ...

  6. UNIX环境高级编程——初始化一个守护进程

    #include <stdio.h> #include <stdlib.h> #include <signal.h> #include <unistd.h&g ...

  7. 使用Geolocation校正GDAL不支持的数据

    对于低分数据来说,常用的校正方式就是给定数据的经纬度查找表来进行校正.在GDAL中,这种校正方式叫Geolocation array.常用的数据有国外的MODIS数据,国内的如风云系列(FY)和海洋系 ...

  8. Java基础---Java---IO流-----LineNumberReader方法及原理、自定义一个LineNumberReader、字节流、图片复制、mp3复制、

    LineNumberReader 跟综行号的缓冲字符输入流,些类定义了setLineNumber(int)和getLineNumber(int),它们可分别用于设置和获取当前行号 import jav ...

  9. acm入门搜索-水池数目

    水池数目 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 校园里有一些小河和一些湖泊,现在,我们把它们通一看成水池,假设有一张我们学校的某处的地图,这个地图上仅标识了此处 ...

  10. 在Android中使用AlarmManager

    AlarmManager是Android中的一种系统级别的提醒服务,它会为我们在特定的时刻广播一个指定的Intent.而使用Intent的时候,我们还需要它执行一个动作,如startActivity, ...