How do I set the default character encoding on my responses to UTF-8?
I\'ve tried this
System.setProperty(\"file.encoding\", \"UTF-8\");
<
The Jetty documentation claims it uses UTF-8 by default, but that seems to be a lie. If you do the normal response.getWrite().println("Hello")
, then the content encoding is determined as follows.
org/eclipse/jetty/http/encoding.properties
: // MimeTypes.java:155
ResourceBundle encoding = ResourceBundle.getBundle("org/eclipse/jetty/http/encoding");
Enumeration i = encoding.getKeys();
while(i.hasMoreElements())
{
String type = i.nextElement();
__encodings.put(type,encoding.getString(type));
}
The default file is:
text/html = ISO-8859-1
text/plain = ISO-8859-1
text/xml = UTF-8
text/json = UTF-8
Response.getWriter()
tries to use that map, but defaults to ISO-8859-1@Override
public PrintWriter getWriter() throws IOException
{
if (_outputType == OutputType.STREAM)
throw new IllegalStateException("STREAM");
if (_outputType == OutputType.NONE)
{
/* get encoding from Content-Type header */
String encoding = _characterEncoding;
if (encoding == null)
{
encoding = MimeTypes.inferCharsetFromContentType(_contentType);
if (encoding == null)
encoding = StringUtil.__ISO_8859_1;
setCharacterEncoding(encoding);
}
So you can see that for text/html
it doesn't default to UTF-8. I don't think there is a way of changing the default from code. The best you can do is change the encoding.properties
file to this:
text/html = UTF-8
text/plain = UTF-8
text/xml = UTF-8
text/json = UTF-8
But even then if it finds an encoding that isn't in there it will default to ISO-8859-1.