您现在的位置: 万盛学电脑网 >> 程序编程 >> 网络编程 >> jsp编程 >> 正文

如何解决JSP参数传递乱码

作者:佚名    责任编辑:admin    更新时间:2022-06-22

  计算机生于美国,英语是他的母语,而英语以外的其它语言对他来说都是外语。他跟我们一样,不管外语掌握到什么程度,也不会像母语那样使用得那么好,时常也会出一些“拼写错误”问题。

       乱码的出现根本原因在于编码和解码使用了不同的编码方案。比如用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代码

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"   
  2.     pageEncoding="UTF-8"%>    
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/lo
  4. ose.dtd">    
  5. <html>    
  6. <head>    
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">    
  8. <title>Insert title here</title>    
  9. </head>    
  10. <body>    
  11. <%-- post方式提交表单 --%>    
  12. <form action="http://localhost:8888/EncodingTest/requestresult.jsp" method="post">    
  13.     UserName:<input type="text" name="username"/>    
  14.     Password:<input type="password" name="password"/>    
  15.     <input type="submit" value="Submit">    
  16. </form>    
  17. </body>    
  18. </html>   
  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!D
  2. OCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loos
  3. e.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <t
  4. itle>Insert title here</title> </head> <body> <%-- post方式提交表单 --%> <form action="http://loc
  5. alhost:8888/EncodingTestb/requestresult.jsp" method="post"> UserName:<input type="text" nam
  6. e="username"/> Password:<input type="password" name="password"/> <input type="submit" va
  7. lue="Submit"> </form> </body> </html>  

在上面的请求页面的username输入框里输入的是“世界杯”三个汉字,password输入框中输入"123"后按下Submit按钮提交请求。截获到的请求报文如下:

Post方式的请求报文代码

  1. POST /EncodingTest/requestresult.jsp HTTP/1.1   
  2. Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, applicati
  3. on/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*    
  4. Referer: http://localhost:8080/TomcatJndiTest/requesttest.jsp    
  5. Accept-Language: zh-cn    
  6. User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; CIBA; aff-kingsoft-ci
  7. ba; .NET CLR 2.0.50727)    
  8. Content-Type: application/x-www-form-urlencoded    
  9. Accept-Encoding: gzip, deflate    
  10. Host: localhost:8888   
  11. Content-Length: 49   
  12. Connection: Keep-Alive    
  13. Cache-Control: no-cache    
  14.    
  15. username=%E4%B8%96%E7%95%8C%E6%9D%AF&password=123   
  16. POST /EncodingTest/requestresult.jsp HTTP/1.1 Accept: image/gif, image/jpeg, image/pjpeg, image/pjp
  17. eg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, applicati
  18. on/msword, */* Referer: http://localhost:8080/TomcatJndiTest/requesttest.jsp Accept-Language: zh-cn Us
  19. er-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; CIBA; aff-kingsoft-ciba; .N
  20. ET CLR 2.0.50727) Content-Type: application/x-www-form-urlencoded Accept-Encoding: gzip, deflate H
  21. ost: localhost:8888 Content-Length: 49 Connection: Keep-Alive Cache-Control: no-cache username=%E
  22. 4%B8%96%E7%95%8C%E6%9D%AF&password=123  

以上报文内容,可以看出post方式的请求报文是有专门的数据部的。,

下面的同一请求页面的get提交方式的请求报文:

Get方式的请求报文代码

  1. GET /EncodingTest/requestresult.jsp?username=%E4%B8%96%E7%95%8C%E6%9D%AF&password=123 H
  2. TTP/1.1   
  3. Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, applica
  4. tion/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*    
  5. Referer: http://localhost:8080/TomcatJndiTest/requesttest.jsp    
  6. Accept-Language: zh-cn    
  7. User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; CIBA; aff-kingsoft-cib
  8. a; .NET CLR 2.0.50727)    
  9. Accept-Encoding: gzip, deflate    
  10. Host: localhost:8888   
  11. Connection: Keep-Alive   
  12. GET /EncodingTest/requestresult.jsp?username=%E4%B8%96%E7%95%8C%E6%9D%AF&passwo
  13. rd=123 HTTP/1.1 Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockw
  14. ave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */* Refer
  15. er: http://localhost:8080/TomcatJndiTest/requesttest.jsp Accept-Language: zh-cn User-Agent: Mozi
  16. lla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; CIBA; aff-kingsoft-ciba; .NET CLR 2.0.50
  17. 727) Accept-Encoding: gzip, deflate&nb