zgqynx 发表于 2013-1-23 02:51:27

中文乱码 我的总结 不断更新

java中文乱码问题

1、jsp中文乱码
   <0> 保证jsp输出到客户端的编码,即显示在客户端时的编码,即在浏览器上显示中文
   <%@page contentType="text/html; charset=gb2312"%>
   <1> jsp 页面,即你的jsp代码中出现中文时
       添加下面语句即可
       <%@page pageEncoding="GBK"%>
   <2> 表单以post方式提交时 <form method="post">.......</form>
2、dojo ajax 提交表单中文乱码
/*回调方法*/ function handler(response){ alert(response);}/*提交表单*/ function submitForm(){dojo.xhrPost({   form: document.forms ,handleAs: "text" ,handle: handler,    sync: true });return false;}
后台代码
new String(request.getParameter("name").getBytes("utf-8"),"GBK");//这种方式没有测试过,或者这样才是对的request.getParameter("name");//dojo 提交表单时,编辑方式为utf-8,

如果是有filter,将编码设置为了 GBK,则应该这样写,即将filter中设置的编码方式再转回来
new String(request.getParameter("name").getBytes("GBK"),"utf-8");//测试过//dojo 提交表单时,编辑方式为utf-8/*这样做不正确,有的汉字还是会乱码,一定要确保在filter中这样才能正确*/request.setCharacterEncoding("utf-8");//可以这样做:String a = request.getQueryString();      if(a==null || a.indexOf("AjaxEncoding=utf-8")==-1){      request.setCharacterEncoding("GBK");      }else{      request.setCharacterEncoding("utf-8");      }
页: [1]
查看完整版本: 中文乱码 我的总结 不断更新