当前位置: 首页 > 图文教程 > 网络编程 > JSP > tomcat下post方式提交服务器乱码

JSP
关于JDBC的介绍
WebSphere应用服务器
JRun2.3平台介绍
Jakarta-Tomcat 简明中文版用户指南
JSWDK环境安装与配置
在Windows NT 4.0下安装Apache+Servlet+JSP
Apache+Tomcat
在WIN2000下的jsp的安装
Tomcat IIS HowTo:将Tomcat装入IIS全攻略
Redhat+apache+jserv+jsdk
安装 WebSphere应用服务器
配置iis和resin1.1
apache+resin
Resin服务器平台介绍简介
在win98下安装JSP环境(jswdk或tomcat在Jdk1.3下)
如何在Windows 9x环境中配置Apache + Tomcat.JSP
98下安装JSP环境的俩常见问题
关于远程方法调用(RMI)的实现
编写跨平台Java程序注意事项
Java编程规则

JSP 中的 tomcat下post方式提交服务器乱码


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-28   浏览: 324 ::
收藏到网摘: n/a

/*
分析:当调用request.getParameter()函数时,会自动进行一次URI的解码过程,
调用时内置的解码过程会导致乱码出现。
而URI 编码两次后,request.getParameter()函数得到的是原信息URI编码一次的内容。
再用可控的解码函数 java.net.URLDecoder.decode()就可解出原始的正确的信息。
*/

var params = encodeURI(encodeURI("method=post&filed=hit&keyword=" + keyword));
createXMLHttpRequest();
var url = "search.do";
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("POST",url,true);
xmlHttp.setRequestHeader("content-length",params.length); 
xmlHttp.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
xmlHttp.setRequestHeader("Connection", "close");
xmlHttp.send(params);

服务器端代码:

response.setContentType("text/html;charset=gb2312");
//ajax post传递的时候,一定是用utf8编码的,url 自己可以设定
String keyword = URLDecoder.decode((String)request.getParameter("keyword"),"utf-8");