Question

I am building a web application, and I have to handle international characters (with stuff like "J'ai surveillé des élève à la rôtule"). Some of the data is in arbitrary static text file that are in an arbitrary directory on the file system. Those files are all utf-8 (thanks standardization!)

To serve this data, I am using an embedded jetty, with the ResourceHandler handler. I don't have any web.xml file. In addition to the static, I have a bunch of restful API that get handled trough servlet.

Problem is, Jetty ResourceHandler class doesn't seem to send a charset along with the static file Content-Type . If I request index.html, Content-Type is text/html. To correctly handle accentuated character, I would like for it to be Content-Type: text/html; charset=utf-8

For file that have a default charset of utf-8 like text/html or text/css, this is fine, but some text file dont have one and get wrongly interpreted as Windows-1252 and the accented character get garbled (I just got a Québec Liquor Store back, instead of Québec Liquor Store). Is there a way to specify a default character set and tell jetty to always send it? Something like apache AddDefaultCharset utf-8

Was it helpful?

Solution

Hardcoding everything to UTF-8 is wrong.

How about just specifying the extension to mime-type mapping for those files you want to control?

MimeTypes mimeTypes = resourceHandler.getMimeTypes();
mimeTypes.addMimeMapping("txt", "text/plain; charset=UTF-8");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top