View Javadoc

1   /*
2   * Copyright 2004 The Apache Software Foundation
3   *
4   * Licensed under the Apache License, Version 2.0 (the "License");
5   * you may not use this file except in compliance with the License.
6   * You may obtain a copy of the License at
7   *
8   *     http://www.apache.org/licenses/LICENSE-2.0
9   *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16  package javax.servlet;
17  
18  import java.io.IOException;
19  import java.io.PrintWriter;
20  import java.util.Locale;
21  
22  
23  /**
24   * Defines an object to assist a servlet in sending a response to the client.
25   * The servlet container creates a <code>ServletResponse</code> object and
26   * passes it as an argument to the servlet's <code>service</code> method.
27   *
28   * <p>To send binary data in a MIME body response, use
29   * the {@link ServletOutputStream} returned by {@link #getOutputStream}.
30   * To send character data, use the <code>PrintWriter</code> object 
31   * returned by {@link #getWriter}. To mix binary and text data,
32   * for example, to create a multipart response, use a
33   * <code>ServletOutputStream</code> and manage the character sections
34   * manually.
35   *
36   * <p>The charset for the MIME body response can be specified
37   * explicitly using the {@link #setCharacterEncoding} and
38   * {@link #setContentType} methods, or implicitly
39   * using the {@link #setLocale} method.
40   * Explicit specifications take precedence over
41   * implicit specifications. If no charset is specified, ISO-8859-1 will be
42   * used. The <code>setCharacterEncoding</code>,
43   * <code>setContentType</code>, or <code>setLocale</code> method must
44   * be called before <code>getWriter</code> and before committing
45   * the response for the character encoding to be used.
46   * 
47   * <p>See the Internet RFCs such as 
48   * <a href="http://www.ietf.org/rfc/rfc2045.txt">
49   * RFC 2045</a> for more information on MIME. Protocols such as SMTP
50   * and HTTP define profiles of MIME, and those standards
51   * are still evolving.
52   *
53   * @author 	Various
54   * @version 	$Version$
55   *
56   * @see		ServletOutputStream
57   *
58   */
59   
60  public interface ServletResponse {
61  
62  
63      
64      /**
65       * Returns the name of the character encoding (MIME charset)
66       * used for the body sent in this response.
67       * The character encoding may have been specified explicitly
68       * using the {@link #setCharacterEncoding} or
69       * {@link #setContentType} methods, or implicitly using the
70       * {@link #setLocale} method. Explicit specifications take
71       * precedence over implicit specifications. Calls made
72       * to these methods after <code>getWriter</code> has been
73       * called or after the response has been committed have no
74       * effect on the character encoding. If no character encoding
75       * has been specified, <code>ISO-8859-1</code> is returned.
76       * <p>See RFC 2047 (http://www.ietf.org/rfc/rfc2047.txt)
77       * for more information about character encoding and MIME.
78       *
79       * @return		a <code>String</code> specifying the
80       *			name of the character encoding, for
81       *			example, <code>UTF-8</code>
82       *
83       */
84    
85      public String getCharacterEncoding();
86      
87      
88  
89      /**
90       * Returns the content type used for the MIME body
91       * sent in this response. The content type proper must
92       * have been specified using {@link #setContentType}
93       * before the response is committed. If no content type
94       * has been specified, this method returns null.
95       * If a content type has been specified and a
96       * character encoding has been explicitly or implicitly
97       * specified as described in {@link #getCharacterEncoding},
98       * the charset parameter is included in the string returned.
99       * If no character encoding has been specified, the
100      * charset parameter is omitted.
101      *
102      * @return		a <code>String</code> specifying the
103      *			content type, for example,
104      *			<code>text/html; charset=UTF-8</code>,
105      *			or null
106      *
107      * @since 2.4
108      */
109   
110     public String getContentType();
111     
112     
113 
114     /**
115      * Returns a {@link ServletOutputStream} suitable for writing binary 
116      * data in the response. The servlet container does not encode the
117      * binary data.  
118      
119      * <p> Calling flush() on the ServletOutputStream commits the response.
120      
121      * Either this method or {@link #getWriter} may 
122      * be called to write the body, not both.
123      *
124      * @return				a {@link ServletOutputStream} for writing binary data	
125      *
126      * @exception IllegalStateException if the <code>getWriter</code> method
127      * 					has been called on this response
128      *
129      * @exception IOException 		if an input or output exception occurred
130      *
131      * @see 				#getWriter
132      *
133      */
134 
135     public ServletOutputStream getOutputStream() throws IOException;
136     
137     
138 
139     /**
140      * Returns a <code>PrintWriter</code> object that
141      * can send character text to the client.
142      * The <code>PrintWriter</code> uses the character
143      * encoding returned by {@link #getCharacterEncoding}.
144      * If the response's character encoding has not been
145      * specified as described in <code>getCharacterEncoding</code>
146      * (i.e., the method just returns the default value 
147      * <code>ISO-8859-1</code>), <code>getWriter</code>
148      * updates it to <code>ISO-8859-1</code>.
149      * <p>Calling flush() on the <code>PrintWriter</code>
150      * commits the response.
151      * <p>Either this method or {@link #getOutputStream} may be called
152      * to write the body, not both.
153      *
154      * 
155      * @return 		a <code>PrintWriter</code> object that 
156      *			can return character data to the client 
157      *
158      * @exception UnsupportedEncodingException
159      *			if the character encoding returned
160      *			by <code>getCharacterEncoding</code> cannot be used
161      *
162      * @exception IllegalStateException
163      *			if the <code>getOutputStream</code>
164      * 			method has already been called for this 
165      *			response object
166      *
167      * @exception IOException
168      *			if an input or output exception occurred
169      *
170      * @see 		#getOutputStream
171      * @see 		#setCharacterEncoding
172      *
173      */
174 
175     public PrintWriter getWriter() throws IOException;
176     
177     
178     
179     
180     /**
181      * Sets the character encoding (MIME charset) of the response
182      * being sent to the client, for example, to UTF-8.
183      * If the character encoding has already been set by
184      * {@link #setContentType} or {@link #setLocale},
185      * this method overrides it.
186      * Calling {@link #setContentType} with the <code>String</code>
187      * of <code>text/html</code> and calling
188      * this method with the <code>String</code> of <code>UTF-8</code>
189      * is equivalent with calling
190      * <code>setContentType</code> with the <code>String</code> of
191      * <code>text/html; charset=UTF-8</code>.
192      * <p>This method can be called repeatedly to change the character
193      * encoding.
194      * This method has no effect if it is called after
195      * <code>getWriter</code> has been
196      * called or after the response has been committed.
197      * <p>Containers must communicate the character encoding used for
198      * the servlet response's writer to the client if the protocol
199      * provides a way for doing so. In the case of HTTP, the character
200      * encoding is communicated as part of the <code>Content-Type</code>
201      * header for text media types. Note that the character encoding
202      * cannot be communicated via HTTP headers if the servlet does not
203      * specify a content type; however, it is still used to encode text
204      * written via the servlet response's writer.
205      *
206      * @param charset 	a String specifying only the character set
207      * 			defined by IANA Character Sets
208      *			(http://www.iana.org/assignments/character-sets)
209      *
210      * @see		#setContentType
211      * 			#setLocale
212      *
213      * @since 2.4
214      *
215      */
216 
217     public void setCharacterEncoding(String charset);
218     
219     
220 
221 
222     /**
223      * Sets the length of the content body in the response
224      * In HTTP servlets, this method sets the HTTP Content-Length header.
225      *
226      *
227      * @param len 	an integer specifying the length of the 
228      * 			content being returned to the client; sets
229      *			the Content-Length header
230      *
231      */
232 
233     public void setContentLength(int len);
234     
235     
236 
237     /**
238      * Sets the content type of the response being sent to
239      * the client, if the response has not been committed yet.
240      * The given content type may include a character encoding
241      * specification, for example, <code>text/html;charset=UTF-8</code>.
242      * The response's character encoding is only set from the given
243      * content type if this method is called before <code>getWriter</code>
244      * is called.
245      * <p>This method may be called repeatedly to change content type and
246      * character encoding.
247      * This method has no effect if called after the response
248      * has been committed. It does not set the response's character
249      * encoding if it is called after <code>getWriter</code>
250      * has been called or after the response has been committed.
251      * <p>Containers must communicate the content type and the character
252      * encoding used for the servlet response's writer to the client if
253      * the protocol provides a way for doing so. In the case of HTTP,
254      * the <code>Content-Type</code> header is used.
255      *
256      * @param type 	a <code>String</code> specifying the MIME 
257      *			type of the content
258      *
259      * @see 		#setLocale
260      * @see 		#setCharacterEncoding
261      * @see 		#getOutputStream
262      * @see 		#getWriter
263      *
264      */
265 
266     public void setContentType(String type);
267     
268 
269     /**
270      * Sets the preferred buffer size for the body of the response.  
271      * The servlet container will use a buffer at least as large as 
272      * the size requested.  The actual buffer size used can be found
273      * using <code>getBufferSize</code>.
274      *
275      * <p>A larger buffer allows more content to be written before anything is
276      * actually sent, thus providing the servlet with more time to set
277      * appropriate status codes and headers.  A smaller buffer decreases 
278      * server memory load and allows the client to start receiving data more
279      * quickly.
280      *
281      * <p>This method must be called before any response body content is
282      * written; if content has been written or the response object has
283      * been committed, this method throws an 
284      * <code>IllegalStateException</code>.
285      *
286      * @param size 	the preferred buffer size
287      *
288      * @exception  IllegalStateException  	if this method is called after
289      *						content has been written
290      *
291      * @see 		#getBufferSize
292      * @see 		#flushBuffer
293      * @see 		#isCommitted
294      * @see 		#reset
295      *
296      */
297 
298     public void setBufferSize(int size);
299     
300     
301 
302     /**
303      * Returns the actual buffer size used for the response.  If no buffering
304      * is used, this method returns 0.
305      *
306      * @return	 	the actual buffer size used
307      *
308      * @see 		#setBufferSize
309      * @see 		#flushBuffer
310      * @see 		#isCommitted
311      * @see 		#reset
312      *
313      */
314 
315     public int getBufferSize();
316     
317     
318 
319     /**
320      * Forces any content in the buffer to be written to the client.  A call
321      * to this method automatically commits the response, meaning the status 
322      * code and headers will be written.
323      *
324      * @see 		#setBufferSize
325      * @see 		#getBufferSize
326      * @see 		#isCommitted
327      * @see 		#reset
328      *
329      */
330 
331     public void flushBuffer() throws IOException;
332     
333     
334     
335     /**
336      * Clears the content of the underlying buffer in the response without
337      * clearing headers or status code. If the 
338      * response has been committed, this method throws an 
339      * <code>IllegalStateException</code>.
340      *
341      * @see 		#setBufferSize
342      * @see 		#getBufferSize
343      * @see 		#isCommitted
344      * @see 		#reset
345      *
346      * @since 2.3
347      */
348 
349     public void resetBuffer();
350     
351 
352     /**
353      * Returns a boolean indicating if the response has been
354      * committed.  A committed response has already had its status 
355      * code and headers written.
356      *
357      * @return		a boolean indicating if the response has been
358      *  		committed
359      *
360      * @see 		#setBufferSize
361      * @see 		#getBufferSize
362      * @see 		#flushBuffer
363      * @see 		#reset
364      *
365      */
366 
367     public boolean isCommitted();
368     
369     
370 
371     /**
372      * Clears any data that exists in the buffer as well as the status code and
373      * headers.  If the response has been committed, this method throws an 
374      * <code>IllegalStateException</code>.
375      *
376      * @exception IllegalStateException  if the response has already been
377      *                                   committed
378      *
379      * @see 		#setBufferSize
380      * @see 		#getBufferSize
381      * @see 		#flushBuffer
382      * @see 		#isCommitted
383      *
384      */
385 
386     public void reset();
387     
388     
389 
390     /**
391      * Sets the locale of the response, if the response has not been
392      * committed yet. It also sets the response's character encoding
393      * appropriately for the locale, if the character encoding has not
394      * been explicitly set using {@link #setContentType} or
395      * {@link #setCharacterEncoding}, <code>getWriter</code> hasn't
396      * been called yet, and the response hasn't been committed yet.
397      * If the deployment descriptor contains a 
398      * <code>locale-encoding-mapping-list</code> element, and that
399      * element provides a mapping for the given locale, that mapping
400      * is used. Otherwise, the mapping from locale to character
401      * encoding is container dependent.
402      * <p>This method may be called repeatedly to change locale and
403      * character encoding. The method has no effect if called after the
404      * response has been committed. It does not set the response's
405      * character encoding if it is called after {@link #setContentType}
406      * has been called with a charset specification, after
407      * {@link #setCharacterEncoding} has been called, after
408      * <code>getWriter</code> has been called, or after the response
409      * has been committed.
410      * <p>Containers must communicate the locale and the character encoding
411      * used for the servlet response's writer to the client if the protocol
412      * provides a way for doing so. In the case of HTTP, the locale is
413      * communicated via the <code>Content-Language</code> header,
414      * the character encoding as part of the <code>Content-Type</code>
415      * header for text media types. Note that the character encoding
416      * cannot be communicated via HTTP headers if the servlet does not
417      * specify a content type; however, it is still used to encode text
418      * written via the servlet response's writer.
419      * 
420      * @param loc  the locale of the response
421      *
422      * @see 		#getLocale
423      * @see 		#setContentType
424      * @see 		#setCharacterEncoding
425      *
426      */
427 
428     public void setLocale(Locale loc);
429     
430     
431 
432     /**
433      * Returns the locale specified for this response
434      * using the {@link #setLocale} method. Calls made to
435      * <code>setLocale</code> after the response is committed
436      * have no effect. If no locale has been specified,
437      * the container's default locale is returned.
438      * 
439      * @see 		#setLocale
440      *
441      */
442 
443     public Locale getLocale();
444 
445 
446 
447 }
448 
449 
450 
451 
452