UTF-8 编码和 http 参数 [英] UTF-8 encoding and http parameters

查看:49
本文介绍了UTF-8 编码和 http 参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 YahooUI Javascript 库进行简单的 ajax 调用,如下所示:

I am doing a simple ajax call with the YahooUI Javascript library as follows:

YAHOO.util.Connect.setForm('myform');
YAHOO.util.Connect.asyncRequest('POST', url, ...);

以下是我的应用程序中的设置:Tomcat 版本:6.0.18

Following are the settings in my app: Tomcat version: 6.0.18

Tomcat 服务器连接器:URIEncoding="UTF-8"网页应用页面:

Tomcat server connector : URIEncoding="UTF-8" webapp page :

在 YahooUI 连接器库文档中也有说明:

Also stated in YahooUI connector library docs:

setForm 将对每个 HTML 表单进行编码字段的名称和值使用编码URI组件.这导致一个UTF-8 编码的字符串,名称-值对.注意:设置 HTTP 标头内容类型"具有不同的字符集值不会改变序列化数据的编码.序列化数据的编码.

setForm will encode each HTML form field's name and value using encodeURIComponent. This results in a string of UTF-8 encoded, name-value pairs. NOTE: Setting an HTTP header of "Content-Type" with a different charset value will not change the encoding of the serialized data.encoding of the serialized data.

我可以看到作为参数发送的法语字符被编码(在 ie7 中使用 iehttpheader 工具):

I can see that the french characters that are being sent as parameters are encoded (in ie7 using iehttpheader tool):

    name=%C3%88%C3%A0%C3%B4 
    testParam=%C3%B4%C3%B4   

对于数据:名称:Èàô和 testParam:ôô

For the data : name: Èàô and testParam: ôô

但在服务器端,我看到的值如下:ÈàÃÂ

But on the server side I am seeing values as following: ÈàÃÂ

即使我将字符串转换为字节,然后使用如下定义的字符集创建新字符串:String val = new String(oo.getBytes("UTF-8"));我无法按预期获得确切数据.

Even if I am converting the string to bytes and then create new string with the charset defined as follows: String val = new String(oo.getBytes("UTF-8")); I am not able to get the exact data as expected.

注意:我参考了下面的问题,但无法解决此问题:如何让 UTF-8 在 Java webapps 中工作?请指导.

Note: I have referenced the question below, but was not able to resolve this issue: How to get UTF-8 working in Java webapps? Please guide.

更新:调用 W3.org 站点上给出的解码 UTF-8 函数 http://www.w3.org/International/O-URL-code.html 给了我预期的结果.我希望 Tomcat 能够解码这个?.

UPDATE: Calling the decode UTF-8 function as given on W3.org site http://www.w3.org/International/O-URL-code.html is giving me the expected results. I expect Tomcat to be decoding this?.

推荐答案

由于您使用的是 POST 查询,URIEncoding="UTF-8" 在这里不适用.你需要设置一个过滤器来告诉Tomcat你的请求编码是UTF-8.例如,您可以使用 Spring 的 CharacterEncodingFilter (用法, javadoc).还可以找到此类过滤器的其他实现.

Since you are using a POST query, URIEncoding="UTF-8" is not applicable here. You need to set a filter to tell Tomcat that your request encoding is UTF-8. You may use, for example, a Spring's CharacterEncodingFilter (usage, javadoc). Other implementations of such filters can also be found.

这篇关于UTF-8 编码和 http 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆