下面是处理页面(processjsp)代码 <%@ page contentType=text/html; charset=gb%>
<html>
<head>
<title>JSP的中文处理</title>
<meta httpequiv=ContentType content=text/html; charset=gb>
</head>
<body>
<%=requestgetParameter(name)%>
</body>
</html>
如果submitjsp提交英文字符能正确显示如果提交中文时就会出现乱码原因浏览器默认使用UTF编码方式来发送请求而UTF和GB编码方式表示字符时不一样这样就出现了不能识别字符解决办法通过requestseCharacterEncoding(gb)对请求进行统一编码就实现了中文的正常显示修改后的processjsp代码如下
<%@ page contentType=text/html; charset=gb%>
<%
requestseCharacterEncoding(gb);
%>
<html>
<head>
<title>JSP的中文处理</title>
<meta httpequiv=ContentType content=text/html; charset=gb>
</head>
<body>
<%=requestgetParameter(name)%>
</body>
</html>
三数据库连接出现乱码
只要涉及中文的地方全部是乱码解决办法在数据库的数据库URL中加上useUnicode=true&characterEncoding=GBK就OK了
四数据库的显示乱码
在mysql中varchar类型text类型就会出现中文乱码对于varchar类型把它设为binary属性就可以解决中文问题对于text类型就要用一个编码转换类来处理实现如下
public String isogb(String qs)
{
try{
if (qs == null) return NULL;
else
{
return new String(qsgetBytes(iso)gb);
}
}
catch(Exception e){
Systemerrprintln(isogb error+egetMessage());
}
return NULL;
}
public String gbiso(String qs)
{
try
{
if (qs == null) return NULL;
else {
return new String(qsgetBytes(gb)iso); }
}
catch(Exception e){ Systemerrprintln(gbiso error+egetMessage());}
return NULL;
}
字符存入数据库时用 gbiso()函数将字符从数据库取出时再用 isogb()函数
[] []