Reputation: 92150
Just wonder what is behind the scene. Actually it seems that we can set the encoding with:
response.setContentType("text/html; charset=UTF-8")
response.setCharacterEncoding("UTF-8")
What is the difference?
Upvotes: 35
Views: 50417
Reputation: 403501
The javadoc is pretty clear about the difference:
void setCharacterEncoding(String charset)
Sets the character encoding (MIME charset) of the response being sent to the client, for example, to UTF-8. If the character encoding has already been set bysetContentType(java.lang.String)
orsetLocale(java.util.Locale)
, this method overrides it. CallingsetContentType(java.lang.String)
with the String oftext/html
and calling this method with the String of UTF-8 is equivalent with callingsetContentType
with the String oftext/html; charset=UTF-8
.
void setContentType(String type)
Sets the content type of the response being sent to the client, if the response has not been committed yet. The given content type may include a character encoding specification, for example,text/html;charset=UTF-8
.
Upvotes: 41