UTF-8 and servlets on Tomcat / Linux

I had some problems reading and writing UTF-8 from servlets on Tomcat 6 / Linux. the request and response were utf-8, the browser was utf-8, URIEncoding was installed in server.xml on both sockets and hosts. In short, every known thing for me is in the code itself, and the server configuration is utf-8.

When reading the request, I had to take a byte array from String, and then convert that byte array to String again. When writing a request, I had to write bytes, not String itself, to get the correct answer (otherwise, I get an exception saying that some character is not ASCII is not valid ISO 8859-1).

+2
source share
2 answers

Changing the LANG environment variable is one way to solve the problem.

The official way is to set the character encoding in the sevlet filter: http://wiki.apache.org/tomcat/Tomcat/UTF-8

Some background information: http://www.crazysquirrel.com/computing/general/form-encoding.jspx

+5
source

The solution was to set the LANG environment variable (in my case) en_US.UTF-8 or, possibly, to any other UTF-8 locale. I am still puzzled by the fact that I could not do anything from the code to make the servlet behave correctly. If there is no way to do this, it will be a mistake from my point of view.

0
source

Source: https://habr.com/ru/post/1717223/


All Articles