21
21
import java .io .IOException ;
22
22
import java .io .PrintWriter ;
23
23
import java .io .UnsupportedEncodingException ;
24
+ import java .nio .charset .Charset ;
25
+ import java .nio .charset .StandardCharsets ;
24
26
import java .util .Locale ;
25
27
26
28
/**
38
40
* request, per web-app (using {@link ServletContext#setRequestCharacterEncoding}, deployment descriptor), and per
39
41
* container (for all web applications deployed in that container, using vendor specific configuration). If multiple of
40
42
* the preceding techniques have been employed, the priority is the order listed. For per request, the charset for the
41
- * response can be specified explicitly using the {@link #setCharacterEncoding} and {@link #setContentType} methods, or
42
- * implicitly using the {@link #setLocale} method. Explicit specifications take precedence over implicit specifications.
43
- * If no charset is explicitly specified, ISO-8859-1 will be used. The <code>setCharacterEncoding</code>,
44
- * <code>setContentType</code>, or <code>setLocale</code> method must be called before <code>getWriter</code> and before
45
- * committing the response for the character encoding to be used.
43
+ * response can be specified explicitly using the {@link #setCharacterEncoding(String)},
44
+ * {@link #setCharacterEncoding(Charset)} and {@link #setContentType} methods, or implicitly using the
45
+ * {@link #setLocale} method. Explicit specifications take precedence over implicit specifications. If no charset is
46
+ * explicitly specified, ISO-8859-1 will be used. The <code>setCharacterEncoding</code>, <code>setContentType</code>, or
47
+ * <code>setLocale</code> method must be called before <code>getWriter</code> and before committing the response for the
48
+ * character encoding to be used.
46
49
*
47
50
* <p>
48
51
* See the Internet RFCs such as <a href="http://www.ietf.org/rfc/rfc2045.txt"> RFC 2045</a> for more information on
@@ -60,10 +63,11 @@ public interface ServletResponse {
60
63
* perweb-app (using {@link ServletContext#setResponseCharacterEncoding}, deployment descriptor), and per container (for
61
64
* all web applications deployed in that container, using vendor specific configuration). The first one of these methods
62
65
* that yields a result is returned. Per-request, the charset for the response can be specified explicitly using the
63
- * {@link #setCharacterEncoding} and {@link #setContentType} methods, or implicitly using the
64
- * setLocale(java.util.Locale) method. Explicit specifications take precedence over implicit specifications. Calls made
65
- * to these methods after <code>getWriter</code> has been called or after the response has been committed have no effect
66
- * on the character encoding. If no character encoding has been specified, <code>ISO-8859-1</code> is returned.
66
+ * {@link #setCharacterEncoding(String)}, {@link #setCharacterEncoding(Charset)} and {@link #setContentType} methods, or
67
+ * implicitly using the setLocale(java.util.Locale) method. Explicit specifications take precedence over implicit
68
+ * specifications. Calls made to these methods after <code>getWriter</code> has been called or after the response has
69
+ * been committed have no effect on the character encoding. If no character encoding has been specified,
70
+ * <code>ISO-8859-1</code> is returned.
67
71
* <p>
68
72
* See RFC 2047 (http://www.ietf.org/rfc/rfc2047.txt) for more information about character encoding and MIME.
69
73
*
@@ -136,18 +140,19 @@ public interface ServletResponse {
136
140
/**
137
141
* Sets the character encoding (MIME charset) of the response being sent to the client, for example, to UTF-8. If the
138
142
* response character encoding has already been set by {@link ServletContext#setResponseCharacterEncoding}, the
139
- * deployment descriptor, or using the {@link #setContentType} or {@link #setLocale} methods, the value set in this
140
- * method overrides all of those values. Calling {@link #setContentType} with the <code>String</code> of
141
- * <code>text/html</code> and calling this method with the <code>String</code> of <code>UTF-8</code> is equivalent to
142
- * calling {@link #setContentType} with the <code>String</code> of <code>text/html; charset=UTF-8</code>.
143
+ * deployment descriptor, or using the {@link #setCharacterEncoding(Charset)}, {@link #setContentType} or
144
+ * {@link #setLocale} methods, the value set in this method overrides all of those values. Calling
145
+ * {@link #setContentType} with the <code>String</code> of <code>text/html</code> and calling this method with the
146
+ * <code>String</code> of <code>UTF-8</code> is equivalent to calling {@link #setContentType} with the
147
+ * <code>String</code> of <code>text/html; charset=UTF-8</code>.
143
148
* <p>
144
149
* This method can be called repeatedly to change the character encoding. This method has no effect if it is called
145
150
* after <code>getWriter</code> has been called or after the response has been committed.
146
151
* <p>
147
152
* If calling this method has an effect (as per the previous paragraph), calling this method with {@code null} clears
148
- * any character encoding set via a previous call to this method, {@link #setContentType} or {@link #setLocale} but does
149
- * not affect any default character encoding configured via {@link ServletContext#setResponseCharacterEncoding} or the
150
- * deployment descriptor.
153
+ * any character encoding set via a previous call to this method, {@link #setCharacterEncoding(Charset)},
154
+ * {@link #setContentType} or {@link #setLocale} but does not affect any default character encoding configured via
155
+ * {@link ServletContext#setResponseCharacterEncoding} or the deployment descriptor.
151
156
* <p>
152
157
* If this method is called with an invalid or unrecognised character encoding, then a subsequent call to
153
158
* {@link #getWriter()} will throw a {@link UnsupportedEncodingException}. Content for an unknown encoding can be sent
@@ -161,15 +166,53 @@ public interface ServletResponse {
161
166
* HTTP headers if the servlet does not specify a content type; however, it is still used to encode text written via the
162
167
* servlet response's writer.
163
168
*
164
- * @param charset a String specifying only the character set defined by IANA Character Sets
169
+ * @param encoding a String specifying only the character set defined by IANA Character Sets
165
170
* (http://www.iana.org/assignments/character-sets) or {@code null}
166
171
*
172
+ * @see #setCharacterEncoding(Charset)
167
173
* @see #setContentType
168
174
* @see #setLocale
169
175
*
170
176
* @since Servlet 2.4
171
177
*/
172
- public void setCharacterEncoding (String charset );
178
+ public void setCharacterEncoding (String encoding );
179
+
180
+ /**
181
+ * Sets the character encoding (MIME charset) of the response being sent to the client, for example, to UTF-8. If the
182
+ * response character encoding has already been set by {@link ServletContext#setResponseCharacterEncoding}, the
183
+ * deployment descriptor, or using the {@link #setCharacterEncoding(String)}, {@link #setContentType} or
184
+ * {@link #setLocale} methods, the value set in this method overrides all of those values. Calling
185
+ * {@link #setContentType} with the <code>String</code> of <code>text/html</code> and calling this method with
186
+ * {@link StandardCharsets#UTF_8} is equivalent to calling {@link #setContentType} with the <code>String</code> of
187
+ * <code>text/html; charset=UTF-8</code>.
188
+ * <p>
189
+ * This method can be called repeatedly to change the character encoding. This method has no effect if it is called
190
+ * after <code>getWriter</code> has been called or after the response has been committed.
191
+ * <p>
192
+ * If calling this method has an effect (as per the previous paragraph), calling this method with {@code null} clears
193
+ * any character encoding set via a previous call to this method, {@link #setCharacterEncoding(String)},
194
+ * {@link #setContentType} or {@link #setLocale} but does not affect any default character encoding configured via
195
+ * {@link ServletContext#setResponseCharacterEncoding} or the deployment descriptor.
196
+ * <p>
197
+ * Containers must communicate the character encoding used for the servlet response's writer to the client if the
198
+ * protocol provides a way for doing so. In the case of HTTP, the character encoding is communicated as part of the
199
+ * <code>Content-Type</code> header for text media types. Note that the character encoding cannot be communicated via
200
+ * HTTP headers if the servlet does not specify a content type; however, it is still used to encode text written via the
201
+ * servlet response's writer.
202
+ * <p>
203
+ * Implementations are strongly encouraged to override this default method and provide a more efficient implementation.
204
+ *
205
+ * @param encoding a Charset instance representing the encoding to use or {@code null}
206
+ *
207
+ * @see #setCharacterEncoding(String)
208
+ * @see #setContentType
209
+ * @see #setLocale
210
+ *
211
+ * @since Servlet 6.1
212
+ */
213
+ default public void setCharacterEncoding (Charset encoding ) {
214
+ setCharacterEncoding (encoding .name ());
215
+ }
173
216
174
217
/**
175
218
* Sets the length of the content body in the response In HTTP servlets, this method sets the HTTP Content-Length
@@ -208,8 +251,9 @@ public interface ServletResponse {
208
251
* <p>
209
252
* If calling this method has an effect (as per the previous paragraph), calling this method with {@code null} clears
210
253
* any content type set via a previous call to this method and clears any character encoding set via a previous call to
211
- * this method, {@link #setCharacterEncoding} or {@link #setLocale} but does not affect any default character encoding
212
- * configured via {@link ServletContext#setResponseCharacterEncoding} or the deployment descriptor.
254
+ * this method, {@link #setCharacterEncoding(String)}, {@link #setCharacterEncoding(Charset)} or {@link #setLocale} but
255
+ * does not affect any default character encoding configured via {@link ServletContext#setResponseCharacterEncoding} or
256
+ * the deployment descriptor.
213
257
* <p>
214
258
* If this method is called with an invalid or unrecognised character encoding, then a subsequent call to
215
259
* {@link #getWriter()} will throw a {@link UnsupportedEncodingException}. Content for an unknown encoding can be sent
@@ -224,7 +268,8 @@ public interface ServletResponse {
224
268
* @param type a <code>String</code> specifying the MIME type of the content or {@code null}
225
269
*
226
270
* @see #setLocale
227
- * @see #setCharacterEncoding
271
+ * @see #setCharacterEncoding(String)
272
+ * @see #setCharacterEncoding(Charset)
228
273
* @see #getOutputStream
229
274
* @see #getWriter
230
275
*
@@ -328,15 +373,16 @@ public interface ServletResponse {
328
373
/**
329
374
* Sets the locale of the response, if the response has not been committed yet. It also sets the response's character
330
375
* encoding appropriately for the locale, if the character encoding has not been explicitly set using
331
- * {@link #setContentType} or {@link #setCharacterEncoding}, <code>getWriter</code> hasn't been called yet, and the
332
- * response hasn't been committed yet. If the deployment descriptor contains a <code>locale-encoding-mapping-list</code>
333
- * element, and that element provides a mapping for the given locale, that mapping is used. Otherwise, the mapping from
334
- * locale to character encoding is container dependent.
376
+ * {@link #setContentType}, {@link #setCharacterEncoding(String)} or {@link #setCharacterEncoding(Charset)},
377
+ * <code>getWriter</code> hasn't been called yet, and the response hasn't been committed yet. If the deployment
378
+ * descriptor contains a <code>locale-encoding-mapping-list</code> element, and that element provides a mapping for the
379
+ * given locale, that mapping is used. Otherwise, the mapping from locale to character encoding is container dependent.
335
380
* <p>
336
381
* This method may be called repeatedly to change locale and character encoding. The method has no effect if called
337
382
* after the response has been committed. It does not set the response's character encoding if it is called after
338
- * {@link #setContentType} has been called with a charset specification, after {@link #setCharacterEncoding} has been
339
- * called, after <code>getWriter</code> has been called, or after the response has been committed.
383
+ * {@link #setContentType} has been called with a charset specification, after {@link #setCharacterEncoding(String} has
384
+ * been called, after {@link #setCharacterEncoding(Charset} has been called, after <code>getWriter</code> has been
385
+ * called, or after the response has been committed.
340
386
* <p>
341
387
* If calling this method has an effect on the locale (as per the previous paragraph), calling this method with
342
388
* {@code null} clears any locale set via a previous call to this method. If calling this method has an effect on the
@@ -352,7 +398,8 @@ public interface ServletResponse {
352
398
*
353
399
* @see #getLocale
354
400
* @see #setContentType
355
- * @see #setCharacterEncoding
401
+ * @see #setCharacterEncoding(String)
402
+ * @see #setCharacterEncoding(Charset)
356
403
*/
357
404
public void setLocale (Locale loc );
358
405
0 commit comments