当调用request.getparameter()函数时,会自动进行一次uri的解码过程,调用时内置的解码过程会导致乱码出现。而uri 编码两次后,request.getparameter()函数得到的是原信息uri编码一次的内容。再用可控的解码函数 java.net.urldecoder.decode()就可解出原始的正确的信息
response.setheader("charset","gb2312");
看到的说明原文如下:
用ajax中采用get来读取页面时,responsetext里面的中文多半会出现乱码,这是因为xmlhttp在处理返回的responsetext的时候,resposebody默认采用utf-8编码,如果服务器送出的确实是utf-8的数据流的时候汉字会正确显示,而读取的页面编码是gbk或者gb2312时就会出现页面中文的乱码。
解决的办法有二:
1、在送出的流里面加一个header,指明送出的是具体编码流:
php教程:header('content-type:text/html;charset=gb2312');
asp教程:response.charset("gb2312")
jsp教程:response.setheader("charset","gb2312");
2、采用utf-8编码来保存html文档
看个ajax jsp乱码解决方法
ajaxpost.js
var myrequest; // variable to hold request object
function mysubmit(){
if (window.xmlhttprequest)
{
myrequest = new xmlhttprequest(); // standards-compliant browsers
} else if (window.activexobject)
{
myrequest = new activexobject("msxml2.xmlhttp"); // for ie
}var post="name="+document.getelementbyid("postval").value;
post=encodeuri(post);
post=encodeuri(post); //最重要的部分,两次调用encodeuri ,就是编码两次
myrequest.open("post","servlet/display",false);
//myrequest.setrequestheader("contentlength",post.length);
myrequest.setrequestheader("content-type","application/x-www-form-urlencoded");
myrequest.send(post);
var res=myrequest.responsetext;//接收返回的数据
document.getelementbyid("display").innerhtml=res;
}
ajaxpost.jsp
<%@ page language="java" import="java.util.*" pageencoding="gb2312"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
<head>
<title>my jsp 'ajaxpost.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page"><script type="text/网页特效" src="js/ajaxpost.js"></script>
</head>
<body>
<textarea rows="10" cols="20" id=postval></textarea>
<br>
<input type=button onclick="mysubmit();" value="打印">
<div id="display"></div>
</body>
</html>
servlet
public void dopost(https教程ervletrequest request, httpservletresponse response)
throws servletexception, ioexception {response.setcontenttype("text/html");
response.setcharacterencoding("gbk");
printwriter out = response.getwriter();
string name=request.getparameter("name");
name=urldecoder.decode(name,"utf8"); //post 传递的时候,一定是用utf8编码的,url 自己可以设定
system.out.println(name);
out.println(name);
out.flush();
out.close();
}
实例二
初始页面内容如下(hello.jsp):
<%@ page language="java" import="java.util.*" pageencoding="gb18030"%>
<%string path = request.getcontextpath();%>
<!doctype html public "-//www.111cn.net//dtd html 4.01 transitional//en">
<html>
<head>
<title>ajax提交页面</title>
<meta http-equiv="content-type" content="text/html; charset=gb18030">
<script type="text/javascript">
function justdo(){
var post="name=王力猛&email=wallimn@sohu.com&bokee=http://down.111cn.net";
post = encodeuri(post);
post = encodeuri(post);//两次,很关键
var xmlobj = new activexobject('msxml2.xmlhttp');
var url = '<%= path%>/page/act.jsp';//文件名需要调整成测试时的相应位置?
xmlobj.open ('post',url,true);
xmlobj.setrequestheader("cache-control","no-cache");
xmlobj.setrequestheader("content-type","application/x-www-form-urlencoded");
xmlobj.send (post);//注意:post方式,使用这个来发送内容?
}
</script>
</head>
<body>
<input type="button" value="提交" onclick="justdo()"/>
</body>
</html>
/////////////////////////////////////////////////////////////////////////////////////
ajax请求处理页面(act.jsp)的内容如下:
/////////////////////////////////////////////////////////////////////////////////////
<%@ page language="java" import="java.util.*" pageencoding="gb18030"%>
<%string path = request.getcontextpath();%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<%@page import="java.net.urldecoder"%>
<html>
<head>
<title>ajax deal</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<%
//遍历输出参数内容。
for (enumeration e = request.getparameternames(); e.hasmoreelements();) {
string h = (string) e.nextelement();
string v = request.getparameter(h);
string mm = java.net.urldecoder.decode(v, "utf-8");
system.out.println("请求参数: " + h + " = " + mm);
}
%>
</body>
</html>
/////////////////////////////////////////////////////////////////////////////////////