当前位置:编程学习 > JS >>

ssh框架中利用Ajax如何解决从服务器传到jsp页面中文乱码问题?

《-----------页面----------》

function createRequest(url){
  alert("进入createRequest()方法");
  http_requst=false;
  if(window.XMLHttpRequest){
  http_request=new XMLHttpRequest();
  }else if(window.ActiveXObject){
  try{
   http_request=new ActiveXObject("Msxml2.XMLHTTP");
  }catch(e){
  try{
     http_request=new ActiveXObject("Microsoft.XMLHTTP");
  }catch(e){}
  }
  }
  if(!http_request){
   alert("不能创建XMLHttpRequest实例对象");
   return false;
  }
  http_request.onreadystatechange=getResult;
  http_request.open("POST",url,true);
  http_request.send(null);
}

function getResult(){
 if(http_request.readyState==4){
 if(http_request.status==200){
 alert(http_request.responseText);
 }
 else{
   alert("您所请求的页面有错误");
 }
 }
}

 

<input type="button" value="检验业主号是否存在" onclick="createRequest('query_pay.do?method=ajax&id='+this.form.pid.value)">

 

 

 

 

《---------action------------------------》

 

 public void ajax(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {
    System.out.println("进入ajax方法");
    String id=request.getParameter("id");
    System.out.println("id="+id);
    int pid=Integer.parseInt(id);
    String hql="select p from TbProprietorinfo as p where pId="+pid;
    TbProprietorinfo p=(TbProprietorinfo)objectDao.getObjectForm(hql);
       if(p==null){
       response.setContentType("text/html");
       PrintWriter out=response.getWriter();
       out.print("该业主不存在,请检查后再输入 !");
       }
 }

在jsp页面得到的弹出框“该业主不存在,请检查后再输入!”为乱码,如何解决?

 

 

追问:我是在struts-config.xml  里面controller元素  processorClass 设置了自定义类 解决了乱码问题 不需要配置过滤器!

答案:你这个乱码很简单的,是因为你response没有设置。
在response.setContentType("text/html");这句话可以去掉,加上一句response.setCharacterEncoding("utf-8");注意那个utf-8是你要转到那个页面的编码格式,如果你的页面的编码是GBK的话就把utf-8改为GBK。这个样子就可以解决了。谢谢。
你的WEB配置文件是否有配置编码格式.
页面的编码格式你改为gbk了吗?

上一个:在注册用户时,怎么利用ajax技术,检验用户名是否已经注册
下一个:谁知道在JAVA Web程序里运用AJAX技术

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,