How do I set the default character encoding on my responses to UTF-8?
I've tried this
System.setProperty("file.encoding", "UTF-8");
and this
System.setProperty("org.eclipse.jetty.util.UrlEncoding.charset", "utf-8");
Neither has any effect - responses are still sent with the header
Content-Type: text/html; charset=ISO-8859-1
I'd like to do this for all text/html responses, and ideally in code rather than XML. I'm using Jetty 9.
You can change the default
UTF-8
charset toISO-8859-1
for example. The documentation does not make it very clear which parameter name for versions later than 9.3. Before 9.3 it wasorg.eclipse.jetty.util.URI.charset
For new versions it has been changed toorg.eclipse.jetty.util.UrlEncoding.charset
Here's an example:content for encode.properties
I created character encoding filter to one legacy application.
In web.xml filter-mapping has the url-pattern of /*. This routes all requests from the web application through the CharacterEncodingFilter.
It matter when you use Writer();
For me If I write
I won't work
But if I change the sequence
It will be alright
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
:The default file is:
Response.getWriter()
tries to use that map, but defaults to ISO-8859-1So 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 theencoding.properties
file to this:But even then if it finds an encoding that isn't in there it will default to ISO-8859-1.