如何强制浏览器在内容类型http头中设置字符集 [英] How to force browser to set charset in content-type http header

查看:1389
本文介绍了如何强制浏览器在内容类型http头中设置字符集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的HTML文件:

 <!DOCTYPE HTML PUBLIC -  // W3C // DTD HTML 4.01 Transitional / / EN> 

< html>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8>
< / head>
< body>
< form method =POSTaction =test.jspaccept-charset =utf-8method =postenctype =application / x-www-form-urlencoded>
< input type =textname =P/>
< input type =submitvalue =subMit/>
< / form>
< / body>
< / html>

HTML文件由服务器使用头 Content-Type:text / html; charset = utf-8 。一切都说:亲爱的浏览器,当你发布此表格,请发布它utf-8编码。浏览器实际上这样做。输入字段中输入的每个值都将进行UTF-8编码。 ,浏览器不会将此告诉服务器! post请求的HTTP头包含一个 Content-Type:application / x-www-form-urlencoded 字段,但字符集将被省略(用FF3.6和问题是我使用的应用程序服务器(Tomcat6)期望在Content-Type头中的字符集(如RFC2388中所述)。像这样: Content-Type:application / x-www-form-urlencoded; charset = utf-8 。如果省略字符集,它将假定ISO-8859-1这不是用于编码的字符集。



有人知道如何强制当前浏览器将字符集附加到Content-Type头部?

解决方案


有人有一个线索如何强制当前浏览器将字符集追加到Content-Type头? / p>

否,没有浏览器提供 charset $ c> application / x-www-form-urlencoded 媒体类型。此外,定义该类型的HTML规范不建议使用 charset 参数,因此服务器不能合理地期望获得一个。



(HTML4 期望 charset multipart / form-data




accept-charset =utf-8


accept-charset 在IE中损坏,用过的。它不会对用作UTF-8的页面中的表单产生影响,但在其他情况下,它可能会导致不一致的结果。



不,你只需要提供它们作为UTF-8的页面,结果应该返回为UTF-8(没有识别标记告诉你,除非潜在的 _charset _ hack ,不支持那个)。



所以你必须告诉Servlet容器参数使用什么编码,如果你不想让它回到默认值在有限的情况下你可以调用 ServletRequest.setCharacterEncoding()来做到这一点,但是这往往是脆弱的,可惜的是,对于Tomcat,你通常需要 muck about with the server.xml ,而不是能够在应用程序中修复它。


A simple HTML file:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<form method="POST" action="test.jsp" accept-charset="utf-8" method="post" enctype="application/x-www-form-urlencoded" >
    <input type="text" name="P"/>
    <input type="submit" value="subMit"/>
</form>
</body>
</html>

The HTML file is served by the server using header Content-Type:text/html; charset=utf-8. Everything says: "dear browser when you post this form, please post it utf-8 encoded". The browser actually does this. Every value entered in the input field will be UTF-8 encoded. BUT the browser wont tell this to the server! The HTTP header of the post request will contain a Content-Type:application/x-www-form-urlencoded field but the charset will be omitted (tested with FF3.6 and IE8).

The problem is the application server I use (Tomcat6) expects the charset in the Content-Type header (as stated in RFC2388). Like this: Content-Type:application/x-www-form-urlencoded;charset=utf-8. If the charset is omitted it will assume ISO-8859-1 which is not the charset used for encoding. The result is broken data.

Does some one have a clue how to force the current browsers to append the charset to the Content-Type header?

解决方案

Does some one have a clue how to force the current browsers to append the charset to the Content-Type header?

No, no browser has ever supplied a charset parameter with the application/x-www-form-urlencoded media type. What's more, the HTML spec which defines that type, does not propose a charset parameter, so the server can't reasonably expect to get one.

(HTML4 does expect a charset for the subparts of a multipart/form-data submission, but even in that case no browser actually complies.)

accept-charset="utf-8"

accept-charset is broken in IE, and shouldn't be used. It won't make a difference either way for forms in pages served as UTF-8, but in other cases it can end up with inconsistent results.

No, with forms you just have to serve the page they're in as UTF-8, and the results should come back as UTF-8 (with no identifying marks to tell you that (except potentially for the _charset_ hack, but Tomcat doesn't support that).

So you have to tell the Servlet container what encoding to use for parameters if you don't want it to fall back to its default (which is usually wrong). In a limited set of circumstances you may be able to call ServletRequest.setCharacterEncoding() to do this, but this tends to be brittle, and doesn't work at all for parameters taken from the query string. There's not a standardised Servlet-level fix for this, sadly. For Tomcat you usually have to muck about with the server.xml instead of being able to fix it in the app.

这篇关于如何强制浏览器在内容类型http头中设置字符集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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