我有一个基于 tomcat 的应用程序,需要提交能够处理 utf-8 字符的表单。通过 ajax 提交时,数据从 utf-8 中的 getParameter() 正确返回。通过表单 post 提交时,数据从 iso-8859-1 中的 getParameter() 返回。
我使用了 fiddler ,并确定了请求中唯一的区别是 charset=utf-8 附加到 Content- 的末尾在 ajax 调用中键入 header (正如预期的那样,因为我显式发送了内容类型)。
来自 ajax 的内容类型: "application/x-www-form-urlencoded; charset=utf-8"
来自表单的ContentType: "应用程序/x-www-form-urlencoded"
我有以下设置:
ajax post(正确输出字符):
$.ajax( {
type : "POST",
url : "blah",
async : false,
contentType: "application/x-www-form-urlencoded; charset=utf-8",
data : data,
success : function(data) {
}
});
form post(输出iso中的字符)
<form id="leadform" enctype="application/x-www-form-urlencoded; charset=utf-8" method="post" accept-charset="utf-8" action="{//app/path}">
xml声明:
<?xml version="1.0" encoding="utf-8"?>
文档类型:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
元标记:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
jvm参数:
-Dfile.encoding=UTF-8
我也尝试过使用 request.setCharacterEncoding("UTF-8"); 但似乎 tomcat 只是忽略了它。我没有使用 RequestDumper 阀门。
根据我的阅读,POST 数据编码主要取决于表单所在的页面编码。据我所知,我的页面以 utf-8 正确编码。
此页面中的示例 JSP 可以正常工作。它只是使用 setCharacterEncoding("UTF-8"); 并回显您发布的数据。 http://wiki.apache.org/tomcat/FAQ/CharacterEncoding
总而言之,尽管页面是 utf-8、指定 utf-8 的表单参数、xml 声明或其他任何内容,但 post 请求不会将字符集作为 utf-8 发送。我花了三天的大部分时间来解决这个问题,但我的想法已经不多了。谁能帮帮我?
最佳答案
form post (outputs chars in iso)
<form id="leadform" enctype="application/x-www-form-urlencoded; charset=utf-8" method="post" accept-charset="utf-8" action="{//app/path}">
您无需在此处指定字符集。浏览器将使用 HTTP 中指定的字符集 响应头。
只是
<form id="leadform" method="post" action="{//app/path}">
够了。
xml declaration:
<?xml version="1.0" encoding="utf-8"?>
无关紧要。它仅与 XML 解析器相关。网络浏览器不解析 text/html
作为 XML。这仅与服务器端相关(如果您使用基于 XML 的 View 技术,如 Facelets 或 JSPX,在纯 JSP 上这是多余的)。
Doctype:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
无关紧要。它仅与 HTML 解析器相关。此外,它没有指定任何字符集。相反,将使用 HTTP 响应 header 中的那个。如果您没有使用 Facelets 或 JSPX 等基于 XML 的 View 技术,这也可以<!DOCTYPE html>
.
meta tag:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
无关紧要。仅当从本地磁盘查看 HTML 页面或要在本地解析时才相关。相反,将使用 HTTP 响应 header 中的那个。
jvm parameters:
-Dfile.encoding=UTF-8
无关紧要。解析源文件只与 Sun/Oracle(!) JVM 相关。
I have also tried using
request.setCharacterEncoding("UTF-8");
but it seems as if tomcat simply ignores it. I am not using the RequestDumper valve.
这仅在请求正文尚未被解析时才有效(即您没有事先调用 getParameter()
等等)。您需要尽早调用它。一个 Filter
是一个完美的地方。否则会被忽略。
From what I've read, POST data encoding is mostly dependent on the page encoding where the form is. As far as I can tell, my page is correctly encoded in utf-8.
这取决于 HTTP 响应 header 。
您需要做的就是以下三件事:
将以下内容添加到您的 JSP 顶部:
<%@page pageEncoding="UTF-8" %>
这会将响应编码设置为 UTF-8,并将响应 header 设置为 UTF-8。
创建一个
Filter
在doFilter()
中执行以下操作方法:if (request.getCharacterEncoding() == null) { request.setCharacterEncoding("UTF-8"); } chain.doFilter(request, response);
这将使 POST 请求正文将被处理为 UTF-8。
更改
<Connector>
进入Tomcat/conf/server.xml
如下:<Connector (...) URIEncoding="UTF-8" />
这将使 GET 查询字符串被处理为 UTF-8。
另见:
- Unicode - How to get characters right? - 包含适用于 Java EE Web 开发人员的实用背景信息和详细解决方案。
关于java - 为什么 POST 不支持字符集,但 AJAX 请求却支持? Tomcat 6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4392610/