计算机生于美国,英语是他的母语,而英语以外的其它语言对他来说都是外语。他跟我们一样,不管外语掌握到什么程度,也不会像母语那样使用得那么好,时常也会出一些“拼写错误”问题。
乱码的出现根本原因在于编码和解码使用了不同的编码方案。比如用GBK编码的文件,用UTF-8去解码结果肯定都是火星文。所以要解决这个问题,中心思想就在于使用统一的编码方案。
jsp页面间的参数传递有以下几种方式:1、表单(form)的提交。2、直接使用URL后接参数的形式(超级链接)。3、如果两个jsp页面在两个不同的窗口中,并且这两个窗口是父子的关系,子窗口中的jsp也可以使用javascript和DOM(window.opener.XXX.value)来取得父窗口中的jsp的输入元素的值。下面就前两种方式中出现的乱码问题做一下剖析。
1、表单(form)的提交实现参数页面间的传递
在介绍表单传递参数的内容之前,先来了解一些预备知识。表单的提交方式和请求报文中对汉字的处理。
表单的提交方式:
通常使用的表单的提交方式主要是:post和get两种。两者的区别在于:post方式是把数据内容放在请求的数据正文部分,没有长度的限制;get方式则是把数据内容直接跟在请求的头部的URL后面,有长度的限制。下面是同一个页面两种方式的请求报许文。
Requesttest.jsp代码
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/lo
- ose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Insert title here</title>
- </head>
- <body>
- <%-- post方式提交表单 --%>
- <form action="http://localhost:8888/EncodingTest/requestresult.jsp" method="post">
- UserName:<input type="text" name="username"/>
- Password:<input type="password" name="password"/>
- <input type="submit" value="Submit">
- </form>
- </body>
- </html>
- <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!D
- OCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loos
- e.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <t
- itle>Insert title here</title> </head> <body> <%-- post方式提交表单 --%> <form action="http://loc
- alhost:8888/EncodingTestb/requestresult.jsp" method="post"> UserName:<input type="text" nam
- e="username"/> Password:<input type="password" name="password"/> <input type="submit" va
- lue="Submit"> </form> </body> </html>
在上面的请求页面的username输入框里输入的是“世界杯”三个汉字,password输入框中输入"123"后按下Submit按钮提交请求。截获到的请求报文如下:
Post方式的请求报文代码
- POST /EncodingTest/requestresult.jsp HTTP/1.1
- Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, applicati
- on/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
- Referer: http://localhost:8080/TomcatJndiTest/requesttest.jsp
- Accept-Language: zh-cn
- User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; CIBA; aff-kingsoft-ci
- ba; .NET CLR 2.0.50727)
- Content-Type: application/x-www-form-urlencoded
- Accept-Encoding: gzip, deflate
- Host: localhost:8888
- Content-Length: 49
- Connection: Keep-Alive
- Cache-Control: no-cache
- username=%E4%B8%96%E7%95%8C%E6%9D%AF&password=123
- POST /EncodingTest/requestresult.jsp HTTP/1.1 Accept: image/gif, image/jpeg, image/pjpeg, image/pjp
- eg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, applicati
- on/msword, */* Referer: http://localhost:8080/TomcatJndiTest/requesttest.jsp Accept-Language: zh-cn Us
- er-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; CIBA; aff-kingsoft-ciba; .N
- ET CLR 2.0.50727) Content-Type: application/x-www-form-urlencoded Accept-Encoding: gzip, deflate H
- ost: localhost:8888 Content-Length: 49 Connection: Keep-Alive Cache-Control: no-cache username=%E
- 4%B8%96%E7%95%8C%E6%9D%AF&password=123
以上报文内容,可以看出post方式的请求报文是有专门的数据部的。,
下面的同一请求页面的get提交方式的请求报文:
Get方式的请求报文代码
- GET /EncodingTest/requestresult.jsp?username=%E4%B8%96%E7%95%8C%E6%9D%AF&password=123 H
- TTP/1.1
- Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, applica
- tion/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
- Referer: http://localhost:8080/TomcatJndiTest/requesttest.jsp
- Accept-Language: zh-cn
- User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; CIBA; aff-kingsoft-cib
- a; .NET CLR 2.0.50727)
- Accept-Encoding: gzip, deflate
- Host: localhost:8888
- Connection: Keep-Alive
- GET /EncodingTest/requestresult.jsp?username=%E4%B8%96%E7%95%8C%E6%9D%AF&passwo
- rd=123 HTTP/1.1 Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockw
- ave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */* Refer
- er: http://localhost:8080/TomcatJndiTest/requesttest.jsp Accept-Language: zh-cn User-Agent: Mozi
- lla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; CIBA; aff-kingsoft-ciba; .NET CLR 2.0.50
- 727) Accept-Encoding: gzip, deflate&nb