简体   繁体   English

Java应用程序服务器如何决定在HTTP Content-type标头中发送什么字符集?

[英]How does a Java application server decide what charset to send in the HTTP Content-type header?

In my current project , I've been forgetting about character encoding and just letting the application server do the right thing. 我目前的项目中 ,我一直忘记字符编码,只是让应用服务器做正确的事情。 This was working fine for some time. 这个工作正常一段时间了。 For instance, I was able to switch the locale in my application to Simplified Chinese / China and see the UTF-8 characters just fine in any recent Firefox or Chrome browser. 例如,我能够将我的应用程序中的语言环境切换到简体中文/中国,并在任何最近的Firefox或Chrome浏览器中看到UTF-8字符。 Something changed (I'm still trying to figure out what). 有些事情发生了变化(我还在试图弄清楚是什么)。 Content is still UTF-8 encoded, but sometimes the Content-type header is setting the charset wrong : 内容仍然是UTF-8编码,但有时Content-type标头设置charset错误

Content-Type:text/html;charset=ISO-8859-1

...leading to garbage appearing in the browser instead of Chinese characters. ...导致垃圾出现在浏览器中而不是中文字符。 If I temporarily force the encoding to UTF-8 in my browser, the characters show up properly. 如果我在浏览器中暂时强制编码为UTF-8,则字符会正确显示。

What would really help nail down the problem is knowing how an application server (or Web dev framework like struts or spring?) decides what to put in this header! 真正有助于解决问题的是知道应用程序服务器(或像struts或spring的Web开发框架?)如何决定放入此标头的内容! Any ideas? 有任何想法吗?

What web technology are you using? 你使用什么网络技术? For instance in servlets you have ServletResponse#setCharacterEncoding() . 例如,在servlet中,您有ServletResponse #setCharacterEncoding() In JSP: 在JSP中:

<%@ page contentType="text/html; charset=UTF-8" %>

Eventually every web framework will end up using one of the two above. 最终,每个Web框架最终都将使用上述两个中的一个。

暂无
暂无

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

相关问题 如何在IBM HTTPD服务器上设置Content-Type HTTP标头的字符集部分? - How do I set the charset portion of the Content-Type HTTP Header on an IBM HTTPD Server? 在 Java 中解析 Content-Type 标头而不验证字符集 - Parsing a Content-Type header in Java without validating the charset 如何发送请求标题是“内容类型”:“应用程序/ json”在排球时获取 - How to send request Header is “Content-Type”:“application/json” when GET on Volley 如何在 Spring boot(v2.4.2) 中从 Content-Type 响应头中删除 charset=UTF-8 - How to remove charset=UTF-8 from Content-Type response header in Spring boot(v2.4.2) 尝试获取 Spring 启动应用程序以在请求没有“内容类型”时发送错误响应消息 HTTP 请求 header - Trying to get Spring Boot app to send an error response message when request does not have "Content-type" HTTP request header Spring-更改内容类型HTTP标头 - Spring - changing Content-type HTTP header 如何抑制字符集被自动添加到 okhttp 中的 Content-Type - How to suppress Charset being automatically added to Content-Type in okhttp 使用 header Content-Type:multipart/form-data 发送 java POST 请求? - Send the java POST request with header Content-Type:multipart/form-data? 如何使Content-Type标头为可选? - How to make Content-Type header optional? 即使不是文本MIME类型,Undertow也会始终向Content-Type标头添加charset - Undertow always add charset to Content-Type header even if it is not a text MIME type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM