`
zgqynx
  • 浏览: 1340837 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

    博客分类:
  • java
阅读更多
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[0] , 
					 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 楼 jiangmeiwei 2013-07-16  

相关推荐

Global site tag (gtag.js) - Google Analytics