简体   繁体   English

Tomcat / Linux上的UTF-8和Servlet

[英]UTF-8 and Servlets on Tomcat/Linux

I've had some problems with reading and writing UTF-8 from servlets on Tomcat 6 / Linux. 在Tomcat 6 / Linux上从servlet读写UTF-8时遇到了一些问题。 request and response were utf-8, browser was utf-8, URIEncoding was set in server.xml on both connectors and hosts. 请求和响应是utf-8,浏览器是utf-8,在连接器和主机上的server.xml中都设置了URIEncoding。 Ins short, every known thing for me in code itself, and server configuration was utf-8. 简而言之,代码本身对我来说所有已知的东西,服务器配置都是utf-8。

When reading request, I've had to take byte array from String, and then convert that byte array into String again. 在读取请求时,我不得不从String中获取字节数组,然后再次将该字节数组转换为String。 When writing request I've had to write bytes, not String itself, in order to get proper response (otherwise I would get an exception that says some non ASCII character is not valid ISO 8859-1). 在写请求时,为了获得正确的响应,我不得不写字节,而不是String本身(否则,我会得到一个异常,说某些非ASCII字符不是有效的ISO 8859-1)。

Changing the LANG environment variable is one way to solve the problem. 更改LANG环境变量是解决问题的一种方法。

The official way is to set the character encoding in a sevlet filter: http://wiki.apache.org/tomcat/Tomcat/UTF-8 官方方法是在sevlet过滤器中设置字符编码: http ://wiki.apache.org/tomcat/Tomcat/UTF-8

Some background information: http://www.crazysquirrel.com/computing/general/form-encoding.jspx 一些背景信息: http : //www.crazysquirrel.com/computing/general/form-encoding.jspx

Solution was to set LANG environmental variable to (in my case) en_US.UTF-8, or probably any other UTF-8 locale. 解决方案是将LANG环境变量设置为(在我的情况下)en_US.UTF-8或任何其他UTF-8语言环境。 I'm still puzzled with the fact, that I couldn't do anything from code to make my servlet behave properly. 我仍然对这个事实感到困惑,因为我无法通过代码做任何事情来使我的servlet正常运行。 If there is no way to do it, than it's a bug from my point of view. 如果没有办法,从我的角度来看,这是一个错误。

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

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