日期:2014-05-16  浏览次数:20424 次

解决jsp参数传递乱码时的问题
    乱码的出现根本原因在于编码和解码使用了不同的编码方案。比如用GBK编码的文件,用UTF-8去解码结果肯定都是火星文。所以要解决这个问题,中心思想就在于使用统一的编码方案。

    jsp页面间的参数传递有以下几种方式:
1.表单(form)的提交。
2.直接使用URL后接参数的形式(超级链接)。
3.如果两个jsp页面在两个不同的窗口中,并且这两个窗口是父子的关系,子窗口中的jsp也可以使用javascript和DOM(window.opener.XXX.value)来取得父窗口中的jsp的输入元素的值。下面就前两种方式中出现的乱码问题做一下剖析。

1.表单(form)的提交实现参数页面间的传递
在介绍表单传递参数的内容之前,先来了解一些预备知识。表单的提交方式和请求报文中对汉字的处理。

表单(POST)的提交方式:
    通常使用的表单的提交方式主要是: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/loose.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"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.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/EncodingTestb/requestresult.jsp" method="post"> UserName:<input type="text" name="username"/> Password:<input type="password" name="password"/> <input type="submit" value="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, application/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-ciba; .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/pjpeg, application/x-shockwave-flash, application/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-ciba; .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方式的请求报文是有专门的数据处理。

get方式:
引用
GET /EncodingTest/requestresult.jsp?username=%E4%B8%96%E7%95%8C%E6%