001 /** 002 * 003 * Copyright 2003-2004 The Apache Software Foundation 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 // 019 // This source code implements specifications defined by the Java 020 // Community Process. In order to remain compliant with the specification 021 // DO NOT add / change / or delete method signatures! 022 // 023 024 package javax.servlet; 025 026 import java.io.IOException; 027 import java.io.PrintWriter; 028 import java.util.Locale; 029 030 031 /** 032 * Defines an object to assist a servlet in sending a response to the client. 033 * The servlet container creates a <code>ServletResponse</code> object and 034 * passes it as an argument to the servlet's <code>service</code> method. 035 * 036 * <p>To send binary data in a MIME body response, use 037 * the {@link ServletOutputStream} returned by {@link #getOutputStream}. 038 * To send character data, use the <code>PrintWriter</code> object 039 * returned by {@link #getWriter}. To mix binary and text data, 040 * for example, to create a multipart response, use a 041 * <code>ServletOutputStream</code> and manage the character sections 042 * manually. 043 * 044 * <p>The charset for the MIME body response can be specified 045 * explicitly using the {@link #setCharacterEncoding} and 046 * {@link #setContentType} methods, or implicitly 047 * using the {@link #setLocale} method. 048 * Explicit specifications take precedence over 049 * implicit specifications. If no charset is specified, ISO-8859-1 will be 050 * used. The <code>setCharacterEncoding</code>, 051 * <code>setContentType</code>, or <code>setLocale</code> method must 052 * be called before <code>getWriter</code> and before committing 053 * the response for the character encoding to be used. 054 * 055 * <p>See the Internet RFCs such as 056 * <a href="http://www.ietf.org/rfc/rfc2045.txt"> 057 * RFC 2045</a> for more information on MIME. Protocols such as SMTP 058 * and HTTP define profiles of MIME, and those standards 059 * are still evolving. 060 * 061 * @see ServletOutputStream 062 * 063 * @version $Rev: 46019 $ $Date: 2004-09-14 02:56:06 -0700 (Tue, 14 Sep 2004) $ 064 */ 065 public interface ServletResponse { 066 /** 067 * Returns the name of the character encoding (MIME charset) 068 * used for the body sent in this response. 069 * The character encoding may have been specified explicitly 070 * using the {@link #setCharacterEncoding} or 071 * {@link #setContentType} methods, or implicitly using the 072 * {@link #setLocale} method. Explicit specifications take 073 * precedence over implicit specifications. Calls made 074 * to these methods after <code>getWriter</code> has been 075 * called or after the response has been committed have no 076 * effect on the character encoding. If no character encoding 077 * has been specified, <code>ISO-8859-1</code> is returned. 078 * <p>See RFC 2047 (http://www.ietf.org/rfc/rfc2047.txt) 079 * for more information about character encoding and MIME. 080 * 081 * @return a <code>String</code> specifying the 082 * name of the character encoding, for example, <code>UTF-8</code> 083 */ 084 public String getCharacterEncoding(); 085 086 /** 087 * Returns the content type used for the MIME body 088 * sent in this response. The content type proper must 089 * have been specified using {@link #setContentType} 090 * before the response is committed. If no content type 091 * has been specified, this method returns null. 092 * If a content type has been specified and a 093 * character encoding has been explicitly or implicitly 094 * specified as described in {@link #getCharacterEncoding}, 095 * the charset parameter is included in the string returned. 096 * If no character encoding has been specified, the 097 * charset parameter is omitted. 098 * 099 * @return a <code>String</code> specifying the 100 * 101 * content type, for example, <code>text/html; charset=UTF-8</code>, 102 * or null 103 * 104 * @since Servlet 2.4 105 */ 106 public String getContentType(); 107 108 /** 109 * Returns a {@link ServletOutputStream} suitable for writing binary 110 * data in the response. The servlet container does not encode the 111 * binary data. 112 * 113 * <p> Calling flush() on the ServletOutputStream commits the response. 114 * 115 * Either this method or {@link #getWriter} may 116 * be called to write the body, not both. 117 * 118 * @return a {@link ServletOutputStream} for writing binary data 119 * 120 * @exception IllegalStateException if the <code>getWriter</code> method 121 * has been called on this response 122 * 123 * @exception IOException if an input or output exception occurred 124 * 125 * @see #getWriter 126 */ 127 public ServletOutputStream getOutputStream() throws IOException; 128 129 /** 130 * Returns a <code>PrintWriter</code> object that 131 * can send character text to the client. 132 * The <code>PrintWriter</code> uses the character 133 * encoding returned by {@link #getCharacterEncoding}. 134 * If the response's character encoding has not been 135 * specified as described in <code>getCharacterEncoding</code> 136 * (i.e., the method just returns the default value 137 * <code>ISO-8859-1</code>), <code>getWriter</code> 138 * updates it to <code>ISO-8859-1</code>. 139 * <p>Calling flush() on the <code>PrintWriter</code> 140 * commits the response. 141 * <p>Either this method or {@link #getOutputStream} may be called 142 * to write the body, not both. 143 * 144 * @return a <code>PrintWriter</code> object that 145 * can return character data to the client 146 * 147 * @exception UnsupportedEncodingException if the character encoding 148 * returned by <code>getCharacterEncoding</code> cannot be used 149 * 150 * @exception IllegalStateException if the <code>getOutputStream</code> 151 * method has already been called for this response object 152 * 153 * @exception IOException if an input or output exception occurred 154 * 155 * @see #getOutputStream 156 * @see #setCharacterEncoding 157 */ 158 public PrintWriter getWriter() throws IOException; 159 160 161 /** 162 * Sets the character encoding (MIME charset) of the response 163 * being sent to the client, for example, to UTF-8. 164 * If the character encoding has already been set by 165 * {@link #setContentType} or {@link #setLocale}, 166 * this method overrides it. 167 * Calling {@link #setContentType} with the <code>String</code> 168 * of <code>text/html</code> and calling 169 * this method with the <code>String</code> of <code>UTF-8</code> 170 * is equivalent with calling 171 * <code>setContentType</code> with the <code>String</code> of 172 * <code>text/html; charset=UTF-8</code>. 173 * <p>This method can be called repeatedly to change the character 174 * encoding. 175 * This method has no effect if it is called after 176 * <code>getWriter</code> has been 177 * called or after the response has been committed. 178 * <p>Containers must communicate the character encoding used for 179 * the servlet response's writer to the client if the protocol 180 * provides a way for doing so. In the case of HTTP, the character 181 * encoding is communicated as part of the <code>Content-Type</code> 182 * header for text media types. Note that the character encoding 183 * cannot be communicated via HTTP headers if the servlet does not 184 * specify a content type; however, it is still used to encode text 185 * written via the servlet response's writer. 186 * 187 * @param charset a String specifying only the character set 188 * defined by IANA Character Sets 189 * (http://www.iana.org/assignments/character-sets) 190 * 191 * @see #setContentType 192 * @see #setLocale 193 * 194 * @since Servlet 2.4 195 */ 196 public void setCharacterEncoding(String charset); 197 198 /** 199 * Sets the length of the content body in the response 200 * In HTTP servlets, this method sets the HTTP Content-Length header. 201 * 202 * @param len an integer specifying the length of the 203 * content being returned to the client; sets 204 * the Content-Length header 205 */ 206 public void setContentLength(int len); 207 208 /** 209 * Sets the content type of the response being sent to 210 * the client, if the response has not been committed yet. 211 * The given content type may include a character encoding 212 * specification, for example, <code>text/html;charset=UTF-8</code>. 213 * The response's character encoding is only set from the given 214 * content type if this method is called before <code>getWriter</code> 215 * is called. 216 * <p>This method may be called repeatedly to change content type and 217 * character encoding. 218 * This method has no effect if called after the response 219 * has been committed. It does not set the response's character 220 * encoding if it is called after <code>getWriter</code> 221 * has been called or after the response has been committed. 222 * <p>Containers must communicate the content type and the character 223 * encoding used for the servlet response's writer to the client if 224 * the protocol provides a way for doing so. In the case of HTTP, 225 * the <code>Content-Type</code> header is used. 226 * 227 * @param type a <code>String</code> specifying the MIME 228 * type of the content 229 * 230 * @see #setLocale 231 * @see #setCharacterEncoding 232 * @see #getOutputStream 233 * @see #getWriter 234 */ 235 public void setContentType(String type); 236 237 /** 238 * Sets the preferred buffer size for the body of the response. 239 * The servlet container will use a buffer at least as large as 240 * the size requested. The actual buffer size used can be found 241 * using <code>getBufferSize</code>. 242 * 243 * <p>A larger buffer allows more content to be written before anything is 244 * actually sent, thus providing the servlet with more time to set 245 * appropriate status codes and headers. A smaller buffer decreases 246 * server memory load and allows the client to start receiving data more 247 * quickly. 248 * 249 * <p>This method must be called before any response body content is 250 * written; if content has been written or the response object has 251 * been committed, this method throws an 252 * <code>IllegalStateException</code>. 253 * 254 * @param size the preferred buffer size 255 * 256 * @exception IllegalStateException if this method is called after 257 * content has been written 258 * 259 * @see #getBufferSize 260 * @see #flushBuffer 261 * @see #isCommitted 262 * @see #reset 263 */ 264 public void setBufferSize(int size); 265 266 /** 267 * Returns the actual buffer size used for the response. If no buffering 268 * is used, this method returns 0. 269 * 270 * @return the actual buffer size used 271 * 272 * @see #setBufferSize 273 * @see #flushBuffer 274 * @see #isCommitted 275 * @see #reset 276 */ 277 public int getBufferSize(); 278 279 /** 280 * Forces any content in the buffer to be written to the client. A call 281 * to this method automatically commits the response, meaning the status 282 * code and headers will be written. 283 * 284 * @see #setBufferSize 285 * @see #getBufferSize 286 * @see #isCommitted 287 * @see #reset 288 */ 289 public void flushBuffer() throws IOException; 290 291 /** 292 * Clears the content of the underlying buffer in the response without 293 * clearing headers or status code. If the 294 * response has been committed, this method throws an 295 * <code>IllegalStateException</code>. 296 * 297 * @see #setBufferSize 298 * @see #getBufferSize 299 * @see #isCommitted 300 * @see #reset 301 * 302 * @since Servlet 2.3 303 */ 304 public void resetBuffer(); 305 306 /** 307 * Returns a boolean indicating if the response has been 308 * committed. A commited response has already had its status 309 * code and headers written. 310 * 311 * @return a boolean indicating if the response has been committed 312 * 313 * @see #setBufferSize 314 * @see #getBufferSize 315 * @see #flushBuffer 316 * @see #reset 317 */ 318 public boolean isCommitted(); 319 320 /** 321 * Clears any data that exists in the buffer as well as the status code and 322 * headers. If the response has been committed, this method throws an 323 * <code>IllegalStateException</code>. 324 * 325 * @exception IllegalStateException if the response has already been 326 * committed 327 * 328 * @see #setBufferSize 329 * @see #getBufferSize 330 * @see #flushBuffer 331 * @see #isCommitted 332 */ 333 public void reset(); 334 335 /** 336 * Sets the locale of the response, if the response has not been 337 * committed yet. It also sets the response's character encoding 338 * appropriately for the locale, if the character encoding has not 339 * been explicitly set using {@link #setContentType} or 340 * {@link #setCharacterEncoding}, <code>getWriter</code> hasn't 341 * been called yet, and the response hasn't been committed yet. 342 * If the deployment descriptor contains a 343 * <code>locale-encoding-mapping-list</code> element, and that 344 * element provides a mapping for the given locale, that mapping 345 * is used. Otherwise, the mapping from locale to character 346 * encoding is container dependent. 347 * <p>This method may be called repeatedly to change locale and 348 * character encoding. The method has no effect if called after the 349 * response has been committed. It does not set the response's 350 * character encoding if it is called after {@link #setContentType} 351 * has been called with a charset specification, after 352 * {@link #setCharacterEncoding} has been called, after 353 * <code>getWriter</code> has been called, or after the response 354 * has been committed. 355 * <p>Containers must communicate the locale and the character encoding 356 * used for the servlet response's writer to the client if the protocol 357 * provides a way for doing so. In the case of HTTP, the locale is 358 * communicated via the <code>Content-Language</code> header, 359 * the character encoding as part of the <code>Content-Type</code> 360 * header for text media types. Note that the character encoding 361 * cannot be communicated via HTTP headers if the servlet does not 362 * specify a content type; however, it is still used to encode text 363 * written via the servlet response's writer. 364 * 365 * @param loc the locale of the response 366 * 367 * @see #getLocale 368 * @see #setContentType 369 * @see #setCharacterEncoding 370 */ 371 public void setLocale(Locale loc); 372 373 /** 374 * Returns the locale specified for this response 375 * using the {@link #setLocale} method. Calls made to 376 * <code>setLocale</code> after the response is committed 377 * have no effect. If no locale has been specified, 378 * the container's default locale is returned. 379 * 380 * @see #setLocale 381 */ 382 public Locale getLocale(); 383 } 384 385 386 387 388