Jetty 9, character encoding UTF-8

你说的曾经没有我的故事 提交于 2020-06-17 01:50:29

问题


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.


回答1:


Looks like you are just missing the pageEncoding attribute.

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



回答2:


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.



来源:https://stackoverflow.com/questions/42637246/jetty-9-character-encoding-utf-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!