2

I have a problem with encoding and Jetty.

All my files are encoded in UTF-8 and include the correct HTML meta tag to specify UTF-8.

Until now all my UTF-8 files had a BOM and I had no problem. But now I am using a different text editor and I noticed that my UTF-8 files are now generated without a BOM which from what I read is rather a good thing so I decided to go without BOM from now.

But the problem is that it seems that Jetty converts all my JSP files to ISO8859-1 before sending them to the browser if they don't have a BOM. It causes problem because since they have a meta tag for UTF-8 the browser interprets the files as UTF-8 and accents and other special characters do not work.

I found one workaround so far which is to start all my JSP files with :

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

This works but it is kindof annoying because I have to add this at the start of every file and I would rather have some server wide parameter to avoid that, if it is possible, but as I spent hours browsing the web for a solution I am beginning to think there is none.

I tried to add

JAVA_OPTIONS+=("-Dfile.encoding=UTF-8")

to my JAVA_OPTIONS when starting jetty as suggested in an other thread but it doesn't seem to do anything.

Any help would be greatly appreciated.

Vincent
  • 51
  • 1
  • 6

2 Answers2

2

Looks like you are just missing the pageEncoding attribute.

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
  • OP is willing to find a way like a wide parameter to set the encoding but not via JSP. –  Apr 30 '17 at 05:48
  • Apache Jasper JSP engine is in full control here. Jetty just follows the servlet behavior and raw output bytes that Apache Jasper JSP engine produces, and sends those bytes to the client unmolested. The only things Jetty can do to that output is to apply `Transfer-Encoding` (chunked) or `Content-Encoding` (gzip). – Joakim Erdfelt May 01 '17 at 12:19
  • Thank you, it works and the explanation helps to understand why it works. It's better than nothing. – gouessej Aug 05 '17 at 22:47
1

Another option that worked for me in the case of handling UTF-8 encoded files on Jetty was to change the webdefault.xml content to support UTF-8 encoding instead of the default ISO-8859-1.

You can find this file in the {{JETTY_HOME}}/etc/webdefault.xml

<locale-encoding-mapping>
  <locale>en</locale>
  <encoding>UTF-8</encoding>
</locale-encoding-mapping>

Hope this helps.