Skip to content

Commit 22c8f34

Browse files
Merge pull request #387 from markt-asf/issue-377
Fix #377 - clarify setCharacterEncoding(null) and related
2 parents 05d57d0 + dbf755b commit 22c8f34

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed

api/src/main/java/jakarta/servlet/ServletResponse.java

+37-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2020 Oracle and/or its affiliates and others.
2+
* Copyright (c) 1997, 2021 Oracle and/or its affiliates and others.
33
* All rights reserved.
44
* Copyright 2004 The Apache Software Foundation
55
*
@@ -20,6 +20,7 @@
2020

2121
import java.io.IOException;
2222
import java.io.PrintWriter;
23+
import java.io.UnsupportedEncodingException;
2324
import java.util.Locale;
2425

2526
/**
@@ -134,23 +135,34 @@ public interface ServletResponse {
134135

135136
/**
136137
* Sets the character encoding (MIME charset) of the response being sent to the client, for example, to UTF-8. If the
137-
* response character encoding has already been set by the {@link ServletContext#setResponseCharacterEncoding},
138-
* deployment descriptor, or using the setContentType() or setLocale() methods, the value set in this method overrides
139-
* any of those values. Calling {@link #setContentType} with the <code>String</code> of <code>text/html</code> and
140-
* calling this method with the <code>String</code> of <code>UTF-8</code> is equivalent with calling
141-
* <code>setContentType</code> with the <code>String</code> of <code>text/html; charset=UTF-8</code>.
138+
* 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>.
142143
* <p>
143144
* This method can be called repeatedly to change the character encoding. This method has no effect if it is called
144145
* after <code>getWriter</code> has been called or after the response has been committed.
145146
* <p>
147+
* 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.
151+
* <p>
152+
* If this method is called with an invalid or unrecognised character encoding, then a subsequent call to
153+
* {@link #getWriter()} will throw a {@link UnsupportedEncodingException}. Content for an unknown encoding can be sent
154+
* with the {@link ServletOutputStream} returned from {@link #getOutputStream()}.
155+
* <p>
156+
* Containers may choose to log calls to this method that use an invalid or unrecognised character encoding.
157+
* <p>
146158
* Containers must communicate the character encoding used for the servlet response's writer to the client if the
147159
* protocol provides a way for doing so. In the case of HTTP, the character encoding is communicated as part of the
148160
* <code>Content-Type</code> header for text media types. Note that the character encoding cannot be communicated via
149161
* HTTP headers if the servlet does not specify a content type; however, it is still used to encode text written via the
150162
* servlet response's writer.
151163
*
152164
* @param charset a String specifying only the character set defined by IANA Character Sets
153-
* (http://www.iana.org/assignments/character-sets)
165+
* (http://www.iana.org/assignments/character-sets) or {@code null}
154166
*
155167
* @see #setContentType
156168
* @see #setLocale
@@ -182,17 +194,28 @@ public interface ServletResponse {
182194
* Sets the content type of the response being sent to the client, if the response has not been committed yet. The given
183195
* content type may include a character encoding specification, for example, <code>text/html;charset=UTF-8</code>. The
184196
* response's character encoding is only set from the given content type if this method is called before
185-
* <code>getWriter</code> is called.
197+
* {@link #getWriter()} is called.
186198
* <p>
187199
* This method may be called repeatedly to change content type and character encoding. This method has no effect if
188200
* called after the response has been committed. It does not set the response's character encoding if it is called after
189201
* <code>getWriter</code> has been called or after the response has been committed.
190202
* <p>
203+
* If calling this method has an effect (as per the previous paragraph), calling this method with {@code null} clears
204+
* any content type set via a previous call to this method and clears any character encoding set via a previous call to
205+
* this method, {@link #setContentType} or {@link #setLocale} but does not affect any default character encoding
206+
* configured via {@link ServletContext#setResponseCharacterEncoding} or the deployment descriptor.
207+
* <p>
208+
* If this method is called with an invalid or unrecognised character encoding, then a subsequent call to
209+
* {@link #getWriter()} will throw a {@link UnsupportedEncodingException}. Content for an unknown encoding can be sent
210+
* with the {@link ServletOutputStream} returned from {@link #getOutputStream()}.
211+
* <p>
212+
* Containers may choose to log calls to this method that use an invalid or unrecognised character encoding.
213+
* <p>
191214
* Containers must communicate the content type and the character encoding used for the servlet response's writer to the
192215
* client if the protocol provides a way for doing so. In the case of HTTP, the <code>Content-Type</code> header is
193216
* used.
194217
*
195-
* @param type a <code>String</code> specifying the MIME type of the content
218+
* @param type a <code>String</code> specifying the MIME type of the content or {@code null}
196219
*
197220
* @see #setLocale
198221
* @see #setCharacterEncoding
@@ -309,13 +332,17 @@ public interface ServletResponse {
309332
* {@link #setContentType} has been called with a charset specification, after {@link #setCharacterEncoding} has been
310333
* called, after <code>getWriter</code> has been called, or after the response has been committed.
311334
* <p>
335+
* If calling this method has an effect on the locale (as per the previous paragraph), calling this method with
336+
* {@code null} clears any locale set via a previous call to this method. If calling this method has an effect on the
337+
* character encoding, calling this method with {@code null} clears the previously set character encoding.
338+
* <p>
312339
* Containers must communicate the locale and the character encoding used for the servlet response's writer to the
313340
* client if the protocol provides a way for doing so. In the case of HTTP, the locale is communicated via the
314341
* <code>Content-Language</code> header, the character encoding as part of the <code>Content-Type</code> header for text
315342
* media types. Note that the character encoding cannot be communicated via HTTP headers if the servlet does not specify
316343
* a content type; however, it is still used to encode text written via the servlet response's writer.
317344
*
318-
* @param loc the locale of the response
345+
* @param loc the locale of the response or {code @null}
319346
*
320347
* @see #getLocale
321348
* @see #setContentType

0 commit comments

Comments
 (0)