网页提交中文到WEB容器的经历了些什么过程....

2022-11-04,,,,

先准备一个网页

<html>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>测试编码</title>
<body>
<form id="form1" name="form1" method="post" action="http://localhost:8080/TestServer/Receive">
<label>
<input type="text" name="name" />
</label>
<label>
<input type="submit" name="SBbt" value="提交" />
</label>
</form>
</body>
</html>

输入中文字,名字   “何锦彬”  进行提交

提交到servelt , 用servelt输出收到的内容

protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
InputStream input = request.getInputStream();
BufferedInputStream inputBf = new BufferedInputStream(input);
byte[] buf = new byte[1024];
int length = inputBf.read(buf);
byte[] receviveData = new byte[length];
System.arraycopy(buf, 0, receviveData, 0, length); System.out.println("收到:" + new String(receviveData)); // System.out.println(request.getParameter("name"));
}

输出如下:

收到:name=%BA%CE%BD%F5%B1%F2&SBbt=%CC%E1%BD%BB

尝试把"何锦彬" 按GBK转成 16进制输出

ffffffba ffffffce ffffffbd fffffff5 ffffffb1 fffffff2 

忽略前面的'f', 会发现,servelt接受到的内容就是: 中文经过GBK编码转码后, 用16进制标识, 并在前面加上"%"以作区分

继续修改修改网页的meta信息,把"GBK" 换成"utf-8"

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

输出如下:

收到:name=%E4%BD%95%E9%94%A6%E5%BD%AC&SBbt=%E6%8F%90%E4%BA%A4

明显 name 后面是9个字符,是用UTF-8转码后16进制表示了字节后再加了'%' 组成

整个过程如下

1, 把中文根据mate 里的字符编码转换成字节

2, 把字节加上百分号并用16进制表示

(1,2部相当于java.net.URLEncoder.encode(content,"utf-8"))

3. 进行传输

4, WEB容器进行解码,相当于调用

TOMCAT中相关代码

org.apache.tomcat.lite.http.HttpRequest    672行

 try {
parameters.add(urlDecode(tmpName, enc),
urlDecode(tmpValue, enc));
} catch (IOException e) {
// ignored
}

网页提交中文到WEB容器的经历了些什么过程....的相关教程结束。

《网页提交中文到WEB容器的经历了些什么过程.....doc》

下载本文的Word格式文档,以方便收藏与打印。