乱码的原因???????????????????

 
开发过程中,经常出现中文乱码的问题,可能一至困扰着您,我现在把我在JSP开发中遇到的中文乱码的问题及解决办法写出来供大家参考。 

  一、JSP页面显示乱码 

  下面的显示页面(display.jsp)就出现乱码: 

< html >  
< head >  
< title > JSP的中文处理 </ title >  
< meta http - equiv = " Content-Type "  content = " text/html; charset=gb2312 " >  
</ head >  

< body >  
<%  
out .print( " JSP的中文处理 " ); 
%>  
</ body >  
</ html >  
  对不同的WEB服务器和不同的JDK版本,处理结果就不一样。原因:服务器使用的编码方式不同和浏览器对不同的字符显示结果不同而导致的。解决办法:在JSP页面中指定编码方式(gb2312),即在页面的第一行加上:
<% @ page contentType = " text/html; charset=gb2312 " %> ,就可以消除乱码了。完整页面如下: 

<% @ page contentType = " text/html; charset=gb2312 " %>  
< html >  
< head >  
< title > JSP的中文处理 </ title >  
< meta http - equiv = " Content-Type "  content = " text/html; charset=gb2312 " >  
</ head >  
< body >  
<%  
out .print( " JSP的中文处理 " ); 
%>  
</ body >  
</ html >  
  二、表单提交中文时出现乱码 

  下面是一个提交页面(submit.jsp),代码如下: 

< html >  
< head >  
< title > JSP的中文处理 </ title >  
< meta http - equiv = " Content-Type "  content = " text/html; charset=gb2312 " >  
</ head >  
< body >  
< form name = " form1 "  method = " post "  action = " process.jsp " >  
< div align = " center " >  
< input type = " text "  name = " name " >  
< input type = " submit "  name = " Submit "  value = " Submit " >  
</ div >  
</ form >  
</ body >  
</ html >  
  下面是处理页面(process.jsp)代码: 

<% @ page contentType = " text/html; charset=gb2312 " %>  
< html >  
< head >  
< title > JSP的中文处理 </ title >  
< meta http - equiv = " Content-Type "  content = " text/html; charset=gb2312 " >  
</ head >  

< body >  
<%= request.getParameter( " name " ) %>  
</ body >  
</ html >  
  如果submit.jsp提交英文字符能正确显示,如果提交中文时就会出现乱码。原因:浏览器默认使用UTF
- 8编码方式来发送请求,而UTF - 8和GB2312编码方式表示字符时不一样,这样就出现了不能识别字符。解决办法:通过request.seCharacterEncoding( " gb2312 " )对请求进行统一编码,就实现了中文的正常显示。修改后的process.jsp代码如下: 

<% @ page contentType = " text/html; charset=gb2312 " %>  
<%  
request.seCharacterEncoding(
" gb2312 " ); 
%>  
< html >  
< head >  
< title > JSP的中文处理 </ title >  
< meta http - equiv = " Content-Type "  content = " text/html; charset=gb2312 " >  
</ head >  
< body >  
<%= request.getParameter( " name " ) %>  
</ body >  
</ html >  
  三、数据库连接出现乱码 

  只要涉及中文的地方全部是乱码,解决办法:在数据库的数据库URL中加上useUnicode
= true & characterEncoding = GBK就OK了。 

  四、数据库的显示乱码 

  在mysql4.
1 .0中,varchar类型,text类型就会出现中文乱码,对于varchar类型把它设为binary属性就可以解决中文问题,对于text类型就要用一个编码转换类来处理,实现如下: 

public   class  Convert 
/** 把ISO-8859-1码转换成GB2312 
*/
 
public static String ISOtoGB(String iso)
String gb; 
try
if(iso.equals(""|| iso == null)
return ""
}
 
else
iso 
= iso.trim(); 
gb 
= new String(iso.getBytes("ISO-8859-1"),"GB2312"); 
return gb; 
}
 
}
 
catch(Exception e)
System.err.print(
"编码转换错误:"+e.getMessage()); 
return ""
}
 
}
 
}

 

 

 

 

 

<%!

public  String codeToString(String str)
{
String s
=str;

try
{
byte tempB[]=s.getBytes("ISO-8859-1");
s
=new String(tempB);
return s;
}

catch(Exception e)
{
return s;
}

}
 
%>


<%

int  flag = 0 ;
String auser_name
= codeToString((String)session.getAttribute( " auser_name " ));
String rank
= codeToString((String)session.getAttribute( " rank " ));



---------------------------------------------
you see
??
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值