ajax乱码问题,100分相送。
一:servlet.java文件内容如下:
package com.lz;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SelectUser extends HttpServlet
{
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{ // 生成一个随机数
//response.setHeader( "Content-Type ", "text/html;charset=utf-8 ");
StringBuffer sb = new StringBuffer();
sb.append( " <?xml version= '1.0 ' encoding= 'utf-8 '?> ");
// 将生成的随机数通过response对象写到客户端去,客户端接收将值写页面的recieveValue中。在这里就可以查询数据库,将查询的结果返回
String a = "select * from user where name= ' "+request.getParameter( "name ")+ " ' and mobile= ' "+request.getParameter( "mobile ")+ " ' " ;
System.out.println(a);
try {
DB db=new DB();
java.sql.ResultSet rs = db.executeQuery(a);
sb.append( " <message> ");
while(rs.next()){
sb.append( " <info> "+rs.getString( "address ")+ " </info> ");
sb.append( " <info> "+rs.getString( "postalcode ")+ " </info> ");
sb.append( " <info> "+rs.getString( "phone ")+ " </info> ");
sb.append( " <info> "+rs.getString( "phone1 ")+ " </info> ");
sb.append( " <info> "+rs.getString( "phone2 ")+ " </info> ");
sb.append( " <info> "+rs.getString( "email ")+ " </info> ");
}
sb.append( " <info> "+ " "+ " </info> ");
sb.append( " <info> "+ " "+ " </info> ");
sb.append( " <info> "+ " "+ " </info> ");
sb.append( " <info> "+ " "+ " </info> ");
sb.append( " <info> "+ " "+ " </info> ");
sb.append( " <info> "+ " "+ " </info> ");
sb.append( " </message> ");
db.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//response.setContentType( "text/html;charset=gb2312 ");
//response.setCharacterEncoding( "utf-8 ");
response.setContentType( "text/html; charset=utf-8 ");
response.setHeader( "Cache-Control ", "no-cache ");
response.getWriter().write(sb.toString());
response.getWriter().flush();
}
}
二:jsp页面中的一部分代码(编码也是utf-8)如下:
<script>
function queryStringName()
{
var name = document.getElementById( "name ").value;
return name;
}
function queryStringMobile()
{
var Mobile = document.getElementById( "Mobile ").value;
return Mobile;
}
function SelectUser()
{
var xmlHttp;
//alert(queryStringName());
if(window.XMLHttpRequest)
{
xmlHttp=new XMLHttpRequest()
}
else if(window.ActiveXObject)
{
xmlHttp=new ActiveXObject( "Microsoft.XMLHTTP ")
}
xmlHttp.onreadystatechange=function()
{
if((xmlHttp.readystate==4)&&(xmlHttp.status==200))
{
//text=xmlHttp.responseText;
var xmlDoc=xmlHttp.responseXML;
document.getElementById( "address ").value=xmlDoc.getElementsByTagName( "info ")[0].text;
document.getElementById( "postalcode ").value=xmlDoc.getElementsByTagName( "info ")[1].text;
document.getElementById( "phone ").value=xmlDoc.getElementsByTagName( "info ")[2].text;
document.getElementById( "phone1 ").value=xmlDoc.getElementsByTagName( "info ")[3].text;
document.getElementById( "phone2 ").value=xmlDoc.getElementsByTagName( "info ")[4].text;
document.getElementById( "email ").value=xmlDoc.getElementsByTagName( "info ")[5].text;
// document.getElementById( "address ").value=text;
}
}
xmlHttp.open( "POST ", "SelectUser?name= "+escape(queryStringName())+ "&mobile= "+escape(queryStringMobile()),true);
xmlHttp.setRequestHeader( "Content-Type ", "application/x-www-form-urlencoded ");
//xmlHttp.setHeader( "Content-Type ", "text/html;charset=GB2312 ");
xmlHttp.send();
}
</script>
=====当在页面上输入中文件时,在servlet中的两个参数out出来时,如果是数字,都能正常显示,如果是中文,就会出现null值,请高手看看。
[解决办法]
字符集不统一~ 用统一的编码
要是还是乱码,请先看乱在什么地方,然后再做处理!
[解决办法]
escape(queryStringName())你这样把内容进行了编码,这个时候要像你这么传递值,需要再把一些代表特殊字符的进行替换,否则取不到值.
改成这样:escape(queryStringName()).replace(/\+/g, "%2B ").replace(/%/g, "%25 ");
[解决办法]
页面采用什么编码呀!是不是相同呢?
------解决方案--------------------
编码问题。。。。。用成统一的。。或添加过滤吧。。。。