web browser application and server encoding and decoding

basic concept

Exchange of information is generated encoding, transmission, decoding three processes. Encoding procedure information is converted from one form into another form, as vocal cords encoding human language, converted into sound waves. Is an inverse function of decoding encoded, eardrum receiving acoustic information by decoding the adult brain can be understood based culture.
All text character set is a set of symbols in a cultural context, its role is to provide for all the characters in a culture, and character representation in the information exchange system, in Computer Information Systems is a byte or 01 sequence. This article will be the character set and encoding scheme interoperability at some point, in order to facilitate understanding.
For java web applications, narrow codec process can be simply understood as: the process of encoding information encoded into a text string sequence 01, 01 is to restore the decoded sequence of information as a text string, into what particular coding sequence of 01 It is a character set encoding used to decide, that is, coding scheme.
Garbled coding scheme is adopted can not understand the information, using the wrong encoding scheme to decode the information caused. If you want to understand the true intentions of a piece of information, you have to know coding scheme using information, which is key information exchange, which is why the war years to crack each other telegraph encryption is actually in deciphering each other's coding scheme.

Http protocol layer codec

http协议层的字符集关系到http发送者和接送者采用什么字符集方案解析对方发送的内容。

Browser-side code

请求端常规请求方式主要为form、url、ajax、http组件如HttpClient API。
浏览器存在文档编码方案charset的概念,文档的编码方案等同于文档解码方案,它对文档中发生的请求编码会产生影响。
影响form提交数据的编码的因素包括:form的accept-charset属性、html文档的编码方案即 document.charset。其中,form的accept-charset是否能够有效,依赖具体浏览器的实现,有些浏览器并不支持,如IE。文 档编码方案可以通过document.charset来修改。
文档内的url编码,如iframe的src指定的url,以文档编码方案为准,地址栏的url的编码方案完全取决于具体的浏览器实现,通过HttpClient组件发送请求时,url是能任意指定编码方案的。
ajax发送http请求的url编码方式完全取决于浏览器实现,一般支持以文档编码方案来决定,但是数据体统一采用utf-8,另外,虽然 ajax可以指定header在contenttype说明编码方案,但这种做法不会对url、数据体的编码方案产生任何影响,甚至在有些浏览器中,最终 contenttype中的编码描述都无法真正影响。
另外,header的编码方案是iso-8859-1,这个是http规范。

Decoding the service side

服务端的httpserver需要解码的对象包括:header、url、数据体。
header解码方案是iso-8859-1。
url解码方案通常称为URIEncoding,一般HttpServer会提供相应设置,标准servlet并不提供该接口。jetty默认utf-8字符集来解码,但其他httpserver如tomcat会默认iso-8859-1。
数据体解码在servlet中可以通过request.setCharacterEncoding来设置。一般的,有些httpserver会以characterEncoding>request请求头字符集>utf-8的优先顺序来决定数据体的解码方案。

Server-side coding

服务端httpserver需要编码的对象是:header、数据体。
header的编码方案同样是iso-8859-1。
通常情况下,服务端必须要指定返回数据体的编码方案且要在header中标注编码方案,否则httpserver一般默认iso-8859-1对输出进行编码,而浏览器也无法得知返回数据体的编码方案,只能自行猜测,完全依赖浏览器自己的实现。
response.setCharacterEncoding的职能是告诉httpserver数据体的编码方案,并不会也不应该影响到 header中的编码方案的标注。response.setContentType会影响到header的编码方案的标注,浏览器根据该标识决定解码方 案。对于一个健全的httpserver来说,在同时通过两个方法指定了数据体编码方案和header编码方案标注的情况下,数据体编码方案应该由后者决 定,这样使浏览器端得到的编码信息和服务端真正编码信息一致。另外,一定要注意的是这两个指定编码方案的方法必须在response创建输出流之前调用, 输出流一旦创建,编码方案无法后期指定。

Decoding the browser side

浏览器端对返回进行解码的对象包括:header、数据体。
header的解码方案是iso-8859-1。
浏览器的数据体解码方案依赖返回信息,浏览器首先从返回头header中查找编码方案标注,如果没有标注,在得知返回内容为html内容的话,将从head的meta标签中读取,如果还没找到,浏览器就不知道如何解码,会消极的选择一种解码方案。
在理论上,推荐html文档在meta中声明编码,且编码的声明一定要在文件开始的1024字节内完成,所以最好在head标签开始时立即声明。
文档中通常都会有一些通过url下载的资源文件,如css和js文件,如果资源文件输出时没有在返回头中指定明确的编码方案,浏览器无法得知编码方案,只能以上面介绍到的文档编码方案来进行解码,这也是浏览器容错的最佳策略。

http://blog.sina.com.cn/s/blog_87cb63e50102w2b6.html
https://www.jianshu.com/p/e918a65b617f

Guess you like

Origin www.cnblogs.com/zhangww/p/11359489.html