How to use UTF-8 with tomcat

Tomcat does not correctly encode string literals containing Unicode characters. The problem occurs on a Linux server, but not on my development machine (Windows). It affects only String literals (not strings read from the database or from the file !!!).

  • I set URIEncoding="utf-8" in the Connector tag (server.xml).
  • I used setCharacterEncoding ().
  • I checked the stack trace (no filters that could set the encoding).
  • I set the LANG environment variable
  • I intercepted the HTTP headers and they are correct (Content-Type = text / plain; charset = utf-8)
  • I checked the encoding in the browser and correctly (UTF-8)

None of the above work. 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 in a browser. ÎÎ "Î" Î · νικά Latin

+7
source share
2 answers

You can encode files when javac reads them, passing in -encoding 'utf-8' or -encoding 'iso-8859-1' at compilation. Just make sure it matches any encoding .java files are actually encoded as.

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

-encoding encoding Specify the encoding name of the source file, for example, EUC-JP and UTF-8. If -encoding is not specified, the platform is the default converter.

+4
source

Try setting the system.encoding system property, for example. -Dfile.encoding=utf-8 on the Linux JVM command line

+3
source

All Articles