简体   繁体   English

Java和Tomcat7中的UTF-8解码问题

[英]UTF-8 decoding problems in Java & Tomcat7

I'm sending an AJAX request to the server, where the param value is encoded in the "escape(...)" function. 我正在向服务器发送AJAX请求,其中参数值在“转义(...)”函数中编码。

The Tomcat server (7.0.42) is configured st the receiving Connector has a URIEncoding="UTF-8", in web.xml I have configured the SetCharacterEncodingFilter as follows: Tomcat服务器(7.0.42)已配置为接收连接器具有URIEncoding =“ UTF-8”,在web.xml中,我已如下配置SetCharacterEncodingFilter:

<filter>
    <filter-name>charencode</filter-name>
    <filter-class>
        org.apache.catalina.filters.SetCharacterEncodingFilter
    </filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>charencode</filter-name>
    <url-pattern>*</url-pattern>
</filter-mapping>

, and additionally I have created a filter to encode the response as UTF-8: ,此外,我还创建了一个过滤器,将响应编码为UTF-8:

@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException {
    arg1.setCharacterEncoding("UTF-8");
    arg2.doFilter(arg0, arg1);
}

There is no issue parsing params that come from the Latin charset, but when I tried Russian, request.getParameter(..) returns null. 解析来自拉丁字符集的参数没有问题,但是当我尝试俄语时,request.getParameter(..)返回null。 Additionally, I get this in the logs (suspect it's coming from the SetCharacterEncodingFilter): 另外,我在日志中得到了这个(怀疑它来自SetCharacterEncodingFilter):

INFO: Character decoding failed. Parameter [usersaid] with value [%u044B%u0432%u0430%u044B%u0432%u0430%u044B%u0432%u044B%u0432%u0430%u044B%u0432%u0430%21] has been ignored. Note that the name and value quoted here may be corrupted due to the failed decoding. Use debug level logging to see the original, non-corrupted values.

And there is no DEBUG-level messages to follow (my logger is set up right I believe..) 而且没有后续的调试级别消息(我相信记录器已正确设置。)

Could you please advise? 您能否提一些建议? Will be happy to answer questions! 会很乐意回答问题!

Many thanks, Victor. 非常感谢,维克多。

That string doesn't decode. 该字符串不解码。 Nothing to do with your application server. 与您的应用程序服务器无关。 Try these tools to see for your self: 尝试以下工具以了解自己:

http://www.albionresearch.com/misc/urlencode.php http://meyerweb.com/eric/tools/dencoder/ http://www.albionresearch.com/misc/urlencode.php http://meyerweb.com/eric/tools/dencoder/

So, the error looks like it might be client side. 因此,该错误看起来可能是在客户端。 Make sure you set the encoding correctly when urlencoding. 在进行urlencoding时,请确保正确设置了编码。 You are probably using something else that UTF-8, which is what you should use. 您可能正在使用UTF-8之外的其他东西,这是您应该使用的东西。

Here's a thread on correctly encoding unicode characters: What is the proper way to URL encode Unicode characters? 这是正确编码Unicode字符的主题: URL编码Unicode字符的正确方法是什么?

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

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