热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

我又来了。。。。后台传值中文,前台乱码,怎么解决。。。。

原贴http:topic.csdn.netu201007061362fdfcc5-1e39-41fe-bd6a-c4765f5a7fd0.html这个问题接着上一个的。。。前台有中
原贴http://topic.csdn.net/u/20100706/13/62fdfcc5-1e39-41fe-bd6a-c4765f5a7fd0.html

这个问题接着上一个的。。。前台有中文,传到后台乱码,这个问题我解决了。。。

但是后台JAVA
ServletActionContext.getResponse().getWriter().print("你好")


前台

if(xmlhttp.readyState == 4){
      if(xmlhttp.status == 200){
         var retText = xmlhttp.responseText;
         alert(retText);                -----------------------------------------中文乱码了,变成了???
      }
   }


怎么弄。。。。求救,在线等。。。。自己边找资料边等。。。。

6 个解决方案

#1


。。。。。。。。。。。我太聪明了。。。。。。。。。自己又解决了。。。。。。。。。。。。晕哦。。

#2


JF。。。

#3


JF...

#4


真佩服你!

#5



js传参要解决乱码
xmlhttp.open("GET","login.action?strName=" + encodeURIComponent(strUserName) ,true);
or
xmlhttp.open("GET","login.action?strName=" + encodeURI(encodeURI(strUserName)) ,true);

这样中文参数就被编码传过去了




public String execute() throws Exception {
        //String strName= (String)ServletActionContext.getRequest().getAttribute("strName");    -----问题。。。
         //获取参数的方式不对
        //1、利用strtus2的特性,设置strName的getter、setter方法就会自动帮我们获取或设置参数
        //2、用getParameter获取参数
        String strName= (String)ServletActionContext.getRequest().getParameter("strName"); //这种方式侵入了servlet的api,不建议使用。用setter比较好
        strName = URLEncoder.encode(strName , "utf-8");//前台参数进行转码,这里也需要解码
        if(!"".equals(strName) && strName!= null){
            ServletActionContext.getResponse().getWriter().print("欢迎"+strName);
        }
      return null;
    }




responseText乱码设置response编码格式
response.setCharacterEncoding("utf-8")
request.setCharacterEncoding("utf-8")

#6


i 服了 you !

jf

推荐阅读
author-avatar
于英豪H
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有