java 为什么 POST 不支持字符集,但 AJAX 请求可以?雄猫6

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4392610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 06:09:44  来源:igfitidea点击:

Why does POST not honor charset, but an AJAX request does? tomcat 6

javaajaxtomcatservletscharacter-encoding

提问by Chris

I have a tomcat based application that needs to submit a form capable of handling utf-8 characters. When submitted via ajax, the data is returned correctly from getParameter() in utf-8. When submitting via form post, the data is returned from getParameter() in iso-8859-1.

我有一个基于 tomcat 的应用程序,需要提交一个能够处理 utf-8 字符的表单。当通过ajax提交时,数据从utf-8中的getParameter()正确返回。通过表单提交提交时,数据从 iso-8859-1 中的 getParameter() 返回。

I used fiddler, and have determined the onlydifference in the requests, is that charset=utf-8is appended to the end of the Content-Typeheader in the ajax call (as expected, since I send the content type explicitly).

我使用了 fiddler,并确定了请求中的唯一区别是charset=utf-8附加到ajax 调用中Content-Type标头的末尾(正如预期的那样,因为我明确发送了内容类型)。

ContentType from ajax: "application/x-www-form-urlencoded; charset=utf-8"

来自 ajax 的内容类型:“application/x-www-form-urlencoded; charset=utf-8”

ContentType from form: "application/x-www-form-urlencoded"

来自表单的内容类型:“application/x-www-form-urlencoded”

I have the following settings:

我有以下设置:

ajax post (outputs chars correctly):

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 (outputs chars in iso)

表单发布(以iso格式输出字符)

 <form id="leadform" enctype="application/x-www-form-urlencoded; charset=utf-8" method="post" accept-charset="utf-8" action="{//app/path}">

xml declaration:

xml声明:

<?xml version="1.0" encoding="utf-8"?>

Doctype:

文档类型:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

meta tag:

元标记:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

jvm parameters:

jvm参数:

-Dfile.encoding=UTF-8

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.

我也试过使用request.setCharacterEncoding("UTF-8"); 但似乎tomcat只是忽略了它。我没有使用 RequestDumper 阀。

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.

从我读过的内容来看,POST 数据编码主要取决于表单所在的页面编码。据我所知,我的页面正确编码为 utf-8。

The sample JSP from this page works correctly. It simply uses setCharacterEncoding("UTF-8");and echos the data you post. http://wiki.apache.org/tomcat/FAQ/CharacterEncoding

此页面中的示例 JSP 工作正常。它只是使用setCharacterEncoding("UTF-8"); 并回显您发布的数据。 http://wiki.apache.org/tomcat/FAQ/CharacterEncoding

So to summarize, the post request does not send the charset as being utf-8, despite the page being in utf-8, the form parameters specifying utf-8, the xml declaration or anything else. I have spent the better part of three days on this and am running out of ideas. Can anyone help me?

总而言之,尽管页面是 utf-8、指定 utf-8 的表单参数、xml 声明或其他任何内容,但 post 请求不会将字符集发送为 utf-8。我已经花了三天的大部分时间在这上面,但我的想法已经用完了。谁能帮我?

采纳答案by BalusC

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}">

表单发布(以iso格式输出字符)

<form id="leadform" enctype="application/x-www-form-urlencoded; charset=utf-8" method="post" accept-charset="utf-8" action="{//app/path}">

You don't need to specify the charset there. The browser will use the charset which is specified in HTTP response header.

您不需要在那里指定字符集。浏览器将使用 HTTP 响应头中指定的字符集。

Just

只是

<form id="leadform" method="post" action="{//app/path}">

is enough.

足够。



xml declaration:

<?xml version="1.0" encoding="utf-8"?>

xml声明:

<?xml version="1.0" encoding="utf-8"?>

Irrelevant. It's only relevant for XML parsers. Webbrowsers doesn't parse text/htmlas XML. This is only relevant for the server side (if you're using a XML based view technology like Facelets or JSPX, on plain JSP this is superfluous).

无关。它仅与 XML 解析器相关。Webbrowsers 不会解析text/html为 XML。这仅与服务器端相关(如果您使用基于 XML 的视图技术,如 Facelets 或 JSPX,则在普通 JSP 上这是多余的)。



Doctype:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

文档类型:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Irrelevant. It's only relevant for HTML parsers. Besides, it doesn't specify any charset. Instead, the one in the HTTP response header will be used. If you aren't using a XML based view technology like Facelets or JSPX, this can be as good <!DOCTYPE html>.

无关。它仅与 HTML 解析器相关。此外,它没有指定任何字符集。相反,将使用 HTTP 响应标头中的那个。如果您不使用基于 XML 的视图技术(如 Facelets 或 JSPX),这也同样好<!DOCTYPE html>



meta tag:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

元标记:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

Irrelevant. It's only relevant when the HTML page is been viewed from local disk or is to be parsed locally. Instead, the one in the HTTP response header will be used.

无关。仅当从本地磁盘查看 HTML 页面或在本地解析 HTML 页面时,它才相关。相反,将使用 HTTP 响应标头中的那个。



jvm parameters:

-Dfile.encoding=UTF-8

jvm参数:

-Dfile.encoding=UTF-8

Irrelevant. It's only relevant to Sun/Oracle(!) JVM to parse the source files.

无关。它仅与 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.

我也尝试过使用,request.setCharacterEncoding("UTF-8");但似乎 tomcat 只是忽略了它。我没有使用 RequestDumper 阀。

This will only work when the request body is not been parsed yet (i.e. you haven't called getParameter()and so on beforehand). You need to call this as early as possible. A Filteris a perfect place for this. Otherwise it will be ignored.

这仅在尚未解析请求正文时有效(即您尚未getParameter()事先调用等)。您需要尽早调用此方法。AFilter是一个完美的地方。否则将被忽略。



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.

从我读过的内容来看,POST 数据编码主要取决于表单所在的页面编码。据我所知,我的页面正确编码为 utf-8。

It's dependent on the HTTP response header.

它取决于 HTTP 响应标头。

All you need to do are the following three things:

您需要做的就是以下三件事:

  1. Add the following to top of your JSP:

    <%@page pageEncoding="UTF-8" %>
    

    This will set the response encoding to UTF-8 and set the response header to UTF-8.

  2. Create a Filterwhich does the following in doFilter()method:

    if (request.getCharacterEncoding() == null) {
        request.setCharacterEncoding("UTF-8");
    }
    chain.doFilter(request, response);
    

    This will make that the POST request body will be processed as UTF-8.

  3. Change the <Connector>entry in Tomcat/conf/server.xmlas follows:

    <Connector (...) URIEncoding="UTF-8" />
    

    This will make that the GET query strings will be processed as UTF-8.

  1. 将以下内容添加到 JSP 的顶部:

    <%@page pageEncoding="UTF-8" %>
    

    这会将响应编码设置为 UTF-8,并将响应标头设置为 UTF-8。

  2. 创建一个FilterdoFilter()方法中执行以下操作的方法:

    if (request.getCharacterEncoding() == null) {
        request.setCharacterEncoding("UTF-8");
    }
    chain.doFilter(request, response);
    

    这将使 POST 请求正文将被处理为 UTF-8。

  3. 更改<Connector>条目Tomcat/conf/server.xml如下:

    <Connector (...) URIEncoding="UTF-8" />
    

    这将使 GET 查询字符串将被处理为 UTF-8。

See also:

也可以看看:

回答by srinannapa

Try this :

试试这个 :

How do I change how POST parameters are interpreted? 

POST requests should specify the encoding of the parameters and values they send. Since many clients fail to set an explicit encoding, the default is used (ISO-8859-1). In many cases this is not the preferred interpretation so one can employ a javax.servlet.Filter to set request encodings. Writing such a filter is trivial. Furthermore Tomcat already comes with such an example filter.

POST 请求应该指定它们发送的参数和值的编码。由于许多客户端无法设置显式编码,因此使用默认值 (ISO-8859-1)。在许多情况下,这不是首选的解释,因此可以使用 javax.servlet.Filter 来设置请求编码。编写这样的过滤器是微不足道的。此外,Tomcat 已经带有这样一个示例过滤器。

Please take a look at:

请看一看:

5.x

webapps/servlets-examples/WEB-INF/classes/filters/SetCharacterEncodingFilter.java

webapps/jsp-examples/WEB-INF/classes/filters/SetCharacterEncodingFilter.java

6.x

webapps/examples/WEB-INF/classes/filters/SetCharacterEncodingFilter.java

For more info , refer to the below URL http://wiki.apache.org/tomcat/FAQ/CharacterEncoding

有关更多信息,请参阅以下 URL http://wiki.apache.org/tomcat/FAQ/CharacterEncoding

回答by Vivin Paliath

Have you tried accept-charset="UTF-8"? As you said, the data should be encoded according to the encoding of the page itself; it seems strange that tomcat is ignoring that. What browser are you trying this out on?

你试过accept-charset="UTF-8"吗?正如你所说,数据应该按照页面本身的编码进行编码;tomcat 忽略了这一点似乎很奇怪。你在什么浏览器上试试这个?

回答by mindas

Have you tried to specify useBodyEncodingForURL="true"in your conf/server.xmlfor HTTP connector?

您是否尝试useBodyEncodingForURL="true"在您conf/server.xml的 HTTP 连接器中指定?

回答by Chris

I implemented a filter based on the information in this postand it is now working. However, this still doesn't explain why even though the page was UTF-8, the charset used by tomcat to interpret it was ISO-9951-1.

我根据这篇文章中的信息实现了一个过滤器,现在它正在工作。然而,这仍然不能解释为什么即使页面是 UTF-8,tomcat 用来解释它的字符集是 ISO-9951-1。