Michael E. Rodriguez | d8af66f | 2005-12-08 20:55:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 1999,2005 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 | |
| 17 | package javax.servlet; |
| 18 | |
| 19 | import java.io.*; |
| 20 | |
| 21 | |
| 22 | /** |
| 23 | * Defines an object to assist a servlet in sending a response to the client. |
| 24 | * The servlet container creates a <code>ServletResponse</code> object and |
| 25 | * passes it as an argument to the servlet's <code>service</code> method. |
| 26 | * |
| 27 | * <p>To send binary data in a MIME body response, use |
| 28 | * the {@link ServletOutputStream} returned by {@link #getOutputStream}. |
| 29 | * To send character data, use the <code>PrintWriter</code> object |
| 30 | * returned by {@link #getWriter}. To mix binary and text data, |
| 31 | * for example, to create a multipart response, use a |
| 32 | * <code>ServletOutputStream</code> and manage the character sections |
| 33 | * manually. |
| 34 | * |
| 35 | * <p>The charset for the MIME body response can be specified with |
| 36 | * {@link #setContentType}. For example, "text/html; charset=Shift_JIS". |
| 37 | * If no charset is specified, ISO-8859-1 will be used. |
| 38 | * The <code>setContentType</code> or <code>setLocale</code> method |
| 39 | * must be called before <code>getWriter</code> for the charset to |
| 40 | * affect the construction of the writer. |
| 41 | * |
| 42 | * <p>See the Internet RFCs such as |
| 43 | * <a href="http://info.internet.isi.edu/in-notes/rfc/files/rfc2045.txt"> |
| 44 | * RFC 2045</a> for more information on MIME. Protocols such as SMTP |
| 45 | * and HTTP define profiles of MIME, and those standards |
| 46 | * are still evolving. |
| 47 | * |
| 48 | * @author Various |
| 49 | * @version $Version$ |
| 50 | * |
| 51 | * @see ServletOutputStream |
| 52 | * |
| 53 | */ |
| 54 | |
| 55 | public interface ServletResponse { |
| 56 | |
| 57 | |
| 58 | |
| 59 | /** |
| 60 | * Returns the name of the charset used for |
| 61 | * the MIME body sent in this response. |
| 62 | * |
| 63 | * <p>If no charset has been assigned, it is implicitly |
| 64 | * set to <code>ISO-8859-1</code> (<code>Latin-1</code>). |
| 65 | * |
| 66 | * <p>See RFC 2047 (http://ds.internic.net/rfc/rfc2045.txt) |
| 67 | * for more information about character encoding and MIME. |
| 68 | * |
| 69 | * @return a <code>String</code> specifying the |
| 70 | * name of the charset, for |
| 71 | * example, <code>ISO-8859-1</code> |
| 72 | * |
| 73 | */ |
| 74 | |
| 75 | public String getCharacterEncoding(); |
| 76 | |
| 77 | |
| 78 | |
| 79 | /** |
| 80 | * Returns a {@link ServletOutputStream} suitable for writing binary |
| 81 | * data in the response. The servlet container does not encode the |
| 82 | * binary data. |
| 83 | |
| 84 | * <p> Calling flush() on the ServletOutputStream commits the response. |
| 85 | |
| 86 | * Either this method or {@link #getWriter} may |
| 87 | * be called to write the body, not both. |
| 88 | * |
| 89 | * @return a {@link ServletOutputStream} for writing binary data |
| 90 | * |
| 91 | * @exception IllegalStateException if the <code>getWriter</code> method |
| 92 | * has been called on this response |
| 93 | * |
| 94 | * @exception IOException if an input or output exception occurred |
| 95 | * |
| 96 | * @see #getWriter |
| 97 | * |
| 98 | */ |
| 99 | |
| 100 | public ServletOutputStream getOutputStream() throws IOException; |
| 101 | |
| 102 | |
| 103 | |
| 104 | /** |
| 105 | * Returns a <code>PrintWriter</code> object that |
| 106 | * can send character text to the client. |
| 107 | * The character encoding used is the one specified |
| 108 | * in the <code>charset=</code> property of the |
| 109 | * {@link #setContentType} method, which must be called |
| 110 | * <i>before</i> calling this method for the charset to take effect. |
| 111 | * |
| 112 | * <p>If necessary, the MIME type of the response is |
| 113 | * modified to reflect the character encoding used. |
| 114 | * |
| 115 | * <p> Calling flush() on the PrintWriter commits the response. |
| 116 | * |
| 117 | * <p>Either this method or {@link #getOutputStream} may be called |
| 118 | * to write the body, not both. |
| 119 | * |
| 120 | * |
| 121 | * @return a <code>PrintWriter</code> object that |
| 122 | * can return character data to the client |
| 123 | * |
| 124 | * @exception UnsupportedEncodingException if the charset specified in |
| 125 | * <code>setContentType</code> cannot be |
| 126 | * used |
| 127 | * |
| 128 | * @exception IllegalStateException if the <code>getOutputStream</code> |
| 129 | * method has already been called for this |
| 130 | * response object |
| 131 | * |
| 132 | * @exception IOException if an input or output exception occurred |
| 133 | * |
| 134 | * @see #getOutputStream |
| 135 | * @see #setContentType |
| 136 | * |
| 137 | */ |
| 138 | |
| 139 | public PrintWriter getWriter() throws IOException; |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | /** |
| 146 | * Sets the length of the content body in the response |
| 147 | * In HTTP servlets, this method sets the HTTP Content-Length header. |
| 148 | * |
| 149 | * |
| 150 | * @param len an integer specifying the length of the |
| 151 | * content being returned to the client; sets |
| 152 | * the Content-Length header |
| 153 | * |
| 154 | */ |
| 155 | |
| 156 | public void setContentLength(int len); |
| 157 | |
| 158 | |
| 159 | |
| 160 | /** |
| 161 | * Sets the content type of the response being sent to |
| 162 | * the client. The content type may include the type of character |
| 163 | * encoding used, for example, <code>text/html; charset=ISO-8859-4</code>. |
| 164 | * |
| 165 | * <p>If obtaining a <code>PrintWriter</code>, this method should be |
| 166 | * called first. |
| 167 | * |
| 168 | * |
| 169 | * @param type a <code>String</code> specifying the MIME |
| 170 | * type of the content |
| 171 | * |
| 172 | * @see #getOutputStream |
| 173 | * @see #getWriter |
| 174 | * |
| 175 | */ |
| 176 | |
| 177 | public void setContentType(String type); |
| 178 | |
| 179 | |
| 180 | |
| 181 | } |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |