1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 1999 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. *
  54. * ====================================================================
  55. *
  56. * This source code implements specifications defined by the Java
  57. * Community Process. In order to remain compliant with the specification
  58. * DO NOT add / change / or delete method signatures!
  59. */
  60. package javax.servlet;
  61. import java.io.IOException;
  62. import java.io.PrintWriter;
  63. import java.io.UnsupportedEncodingException;
  64. import java.util.Locale;
  65. /**
  66. * Defines an object to assist a servlet in sending a response to the client.
  67. * The servlet container creates a <code>ServletResponse</code> object and
  68. * passes it as an argument to the servlet's <code>service</code> method.
  69. *
  70. * <p>To send binary data in a MIME body response, use
  71. * the {@link ServletOutputStream} returned by {@link #getOutputStream}.
  72. * To send character data, use the <code>PrintWriter</code> object
  73. * returned by {@link #getWriter}. To mix binary and text data,
  74. * for example, to create a multipart response, use a
  75. * <code>ServletOutputStream</code> and manage the character sections
  76. * manually.
  77. *
  78. * <p>The charset for the MIME body response can be specified with
  79. * {@link #setContentType}. For example, "text/html; charset=Shift_JIS".
  80. * The charset can alternately be set using {@link #setLocale}.
  81. * If no charset is specified, ISO-8859-1 will be used.
  82. * The <code>setContentType</code> or <code>setLocale</code> method
  83. * must be called before <code>getWriter</code> for the charset to
  84. * affect the construction of the writer.
  85. *
  86. * <p>See the Internet RFCs such as
  87. * <a href="http://info.internet.isi.edu/in-notes/rfc/files/rfc2045.txt">
  88. * RFC 2045</a> for more information on MIME. Protocols such as SMTP
  89. * and HTTP define profiles of MIME, and those standards
  90. * are still evolving.
  91. *
  92. * @author Various
  93. * @version $Version$
  94. *
  95. * @see ServletOutputStream
  96. *
  97. */
  98. public interface ServletResponse {
  99. /**
  100. * Returns the name of the charset used for
  101. * the MIME body sent in this response.
  102. *
  103. * <p>If no charset has been assigned, it is implicitly
  104. * set to <code>ISO-8859-1</code> (<code>Latin-1</code>).
  105. *
  106. * <p>See RFC 2047 (http://ds.internic.net/rfc/rfc2045.txt)
  107. * for more information about character encoding and MIME.
  108. *
  109. * @return a <code>String</code> specifying the
  110. * name of the charset, for
  111. * example, <code>ISO-8859-1</code>
  112. *
  113. */
  114. public String getCharacterEncoding();
  115. /**
  116. * Returns a {@link ServletOutputStream} suitable for writing binary
  117. * data in the response. The servlet container does not encode the
  118. * binary data.
  119. * <p> Calling flush() on the ServletOutputStream commits the response.
  120. * Either this method or {@link #getWriter} may
  121. * be called to write the body, not both.
  122. *
  123. * @return a {@link ServletOutputStream} for writing binary data
  124. *
  125. * @exception IllegalStateException if the <code>getWriter</code> method
  126. * has been called on this response
  127. *
  128. * @exception IOException if an input or output exception occurred
  129. *
  130. * @see #getWriter
  131. *
  132. */
  133. public ServletOutputStream getOutputStream() throws IOException;
  134. /**
  135. * Returns a <code>PrintWriter</code> object that
  136. * can send character text to the client.
  137. * The character encoding used is the one specified
  138. * in the <code>charset=</code> property of the
  139. * {@link #setContentType} method, which must be called
  140. * <i>before</i> calling this method for the charset to take effect.
  141. *
  142. * <p>If necessary, the MIME type of the response is
  143. * modified to reflect the character encoding used.
  144. *
  145. * <p> Calling flush() on the PrintWriter commits the response.
  146. *
  147. * <p>Either this method or {@link #getOutputStream} may be called
  148. * to write the body, not both.
  149. *
  150. *
  151. * @return a <code>PrintWriter</code> object that
  152. * can return character data to the client
  153. *
  154. * @exception UnsupportedEncodingException if the charset specified in
  155. * <code>setContentType</code> cannot be
  156. * used
  157. *
  158. * @exception IllegalStateException if the <code>getOutputStream</code>
  159. * method has already been called for this
  160. * response object
  161. *
  162. * @exception IOException if an input or output exception occurred
  163. *
  164. * @see #getOutputStream
  165. * @see #setContentType
  166. *
  167. */
  168. public PrintWriter getWriter() throws IOException;
  169. /**
  170. * Sets the length of the content body in the response
  171. * In HTTP servlets, this method sets the HTTP Content-Length header.
  172. *
  173. *
  174. * @param len an integer specifying the length of the
  175. * content being returned to the client; sets
  176. * the Content-Length header
  177. *
  178. */
  179. public void setContentLength(int len);
  180. /**
  181. * Sets the content type of the response being sent to
  182. * the client. The content type may include the type of character
  183. * encoding used, for example, <code>text/html; charset=ISO-8859-4</code>.
  184. *
  185. * <p>If obtaining a <code>PrintWriter</code>, this method should be
  186. * called first.
  187. *
  188. *
  189. * @param type a <code>String</code> specifying the MIME
  190. * type of the content
  191. *
  192. * @see #getOutputStream
  193. * @see #getWriter
  194. *
  195. */
  196. public void setContentType(String type);
  197. /**
  198. * Sets the preferred buffer size for the body of the response.
  199. * The servlet container will use a buffer at least as large as
  200. * the size requested. The actual buffer size used can be found
  201. * using <code>getBufferSize</code>.
  202. *
  203. * <p>A larger buffer allows more content to be written before anything is
  204. * actually sent, thus providing the servlet with more time to set
  205. * appropriate status codes and headers. A smaller buffer decreases
  206. * server memory load and allows the client to start receiving data more
  207. * quickly.
  208. *
  209. * <p>This method must be called before any response body content is
  210. * written; if content has been written, this method throws an
  211. * <code>IllegalStateException</code>.
  212. *
  213. * @param size the preferred buffer size
  214. *
  215. * @exception IllegalStateException if this method is called after
  216. * content has been written
  217. *
  218. * @see #getBufferSize
  219. * @see #flushBuffer
  220. * @see #isCommitted
  221. * @see #reset
  222. *
  223. */
  224. public void setBufferSize(int size);
  225. /**
  226. * Returns the actual buffer size used for the response. If no buffering
  227. * is used, this method returns 0.
  228. *
  229. * @return the actual buffer size used
  230. *
  231. * @see #setBufferSize
  232. * @see #flushBuffer
  233. * @see #isCommitted
  234. * @see #reset
  235. *
  236. */
  237. public int getBufferSize();
  238. /**
  239. * Forces any content in the buffer to be written to the client. A call
  240. * to this method automatically commits the response, meaning the status
  241. * code and headers will be written.
  242. *
  243. * @see #setBufferSize
  244. * @see #getBufferSize
  245. * @see #isCommitted
  246. * @see #reset
  247. *
  248. */
  249. public void flushBuffer() throws IOException;
  250. /**
  251. * Clears the content of the underlying buffer in the response without
  252. * clearing headers or status code. If the
  253. * response has been committed, this method throws an
  254. * <code>IllegalStateException</code>.
  255. *
  256. * @see #setBufferSize
  257. * @see #getBufferSize
  258. * @see #isCommitted
  259. * @see #reset
  260. *
  261. * @since 2.3
  262. */
  263. public void resetBuffer();
  264. /**
  265. * Returns a boolean indicating if the response has been
  266. * committed. A commited response has already had its status
  267. * code and headers written.
  268. *
  269. * @return a boolean indicating if the response has been
  270. * committed
  271. *
  272. * @see #setBufferSize
  273. * @see #getBufferSize
  274. * @see #flushBuffer
  275. * @see #reset
  276. *
  277. */
  278. public boolean isCommitted();
  279. /**
  280. * Clears any data that exists in the buffer as well as the status code and
  281. * headers. If the response has been committed, this method throws an
  282. * <code>IllegalStateException</code>.
  283. *
  284. * @exception IllegalStateException if the response has already been
  285. * committed
  286. *
  287. * @see #setBufferSize
  288. * @see #getBufferSize
  289. * @see #flushBuffer
  290. * @see #isCommitted
  291. *
  292. */
  293. public void reset();
  294. /**
  295. * Sets the locale of the response, setting the headers (including the
  296. * Content-Type's charset) as appropriate. This method should be called
  297. * before a call to {@link #getWriter}. By default, the response locale
  298. * is the default locale for the server.
  299. *
  300. * @param loc the locale of the response
  301. *
  302. * @see #getLocale
  303. *
  304. */
  305. public void setLocale(Locale loc);
  306. /**
  307. * Returns the locale assigned to the response.
  308. *
  309. *
  310. * @see #setLocale
  311. *
  312. */
  313. public Locale getLocale();
  314. }