简体   繁体   English

如何在tomcat中使用UTF-8

[英]How to use UTF-8 with tomcat

Tomcat does not encode correctly String literals that contain unicode characters. Tomcat无法正确编码包含unicode字符的字符串文字。 The problem occurs at a Linux server but not on my development machine (Windows). 问题发生在Linux服务器上,但不在我的开发机器上(Windows)。 It affects ONLY String literals (not Strings read from DB or from file!!!). 它仅影响字符串文字(不是从DB或文件中读取的字符串!!!)。

  • I have set the URIEncoding="utf-8" at the Connector tag (server.xml). 我在Connector标签(server.xml)上设置了URIEncoding="utf-8"
  • I have used setCharacterEncoding(). 我使用了setCharacterEncoding()。
  • I cheched the stack trace (no filters that might set encoding). 我收集了堆栈跟踪(没有可能设置编码的过滤器)。
  • I have set the LANG environment variable 我已经设置了LANG环境变量
  • I cheched the HTTP Headers and they are correct (Content-Type=text/plain;charset=utf-8) 我打了HTTP Headers他们是正确的(Content-Type = text / plain; charset = utf-8)
  • I checked the encoding at the browser and it is correct (UTF-8) 我在浏览器上检查了编码,它是正确的(UTF-8)

Nothing of the above works. 上述任何内容都无效。 Any ideas on what I might be missing? 关于我可能遗失的任何想法?

public class Test extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    resp.setCharacterEncoding("utf-8");
    resp.setContentType("text/plain;");

    Writer w = resp.getWriter();
    w.write("Μαλακία Latin"); //Some unicode characters
    w.close();
}

The above shows this at the browser. 以上内容在浏览器中显示。 Îλληνικά Latin ÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎ

You can force the encoding of files when javac reads them by passing in -encoding 'utf-8' or -encoding 'iso-8859-1' when compiling. 当javac通过在编译时传入-encoding'utf-8'或-encoding'iso-8859-1'来强制读取文件时,可以强制进行文件编码。 Just make sure that it matches whatever encoding your .java files are actually encoded as. 只需确保它匹配您的.java文件实际编码为的任何编码。

http://docs.oracle.com/javase/6/docs/technotes/tools/windows/javac.html http://docs.oracle.com/javase/6/docs/technotes/tools/windows/javac.html

-encoding encoding Set the source file encoding name, such as EUC-JP and UTF-8. -encoding encoding设置源文件编码名称,例如EUC-JP和UTF-8。 If -encoding is not specified, the platform default converter is used. 如果未指定-encoding,则使用平台默认转换器。

尝试在Linux JVM命令行上设置file.encoding系统属性,例如-Dfile.encoding=utf-8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM