一定是你编码不符吧。一般都是编码上有问题
一、乱码
1、页面编码 (如:<%@ page contentType="text/html;charset=gb2312" language="java"%>)
2、传参编码 (如:request.getParameter("name"))
3、 jdbc URL 编码 (如:conn=DriverManager.getConnection("jdbc:mysql://localhost: 3306/project1?useUnicode=true&characterEncoding=GBK","admin","adminisme");
4、数据库编码 (如:mysql的安装下有my.ini文件里面:default-character-set=latin1 (默认))
最初我页面上面编码为:<%@ page contentType="text/html;charset=gb2312" language="java"%>;传参没有设置(默认好像是ISO-8859-1);jdbc url也没有设置;数据也没有设置,默认为latin1。我没有用过滤器来过滤编码而是通过一个类来编码转换,因为设计的输入中文的字段比较少,就写了如下类:
import java.io.UnsupportedEncodingException;
import java.lang.String;
public class Strto {
public static String toChinese(String str){
if(str==null||str.length()<1){
str="";
}else{
try{
str=(new String(str.getBytes("ISO-8859-1"),"gb2312"));
}catch(UnsupportedEncodingException e){
e.printStackTrace();
return str;
}
}
return str;
}
public static String toIso(String str){
if(str==null||str.length()<1){
str="";
}else{
try{
str=(new String(str.getBytes("gb2312"),"ISO-8859-1"));
}catch(UnsupportedEncodingException e){
e.printStackTrace();
return str;
}
}
return str;
}
}
页 面输入的中文通过request取得值后在往数据库中送的时候request.getParameter("name")的到的值可直接存入,因为页面输 入的中文是gb2312,通过request.getParameter的到后默认会自动转码为ISO-8859-1,这样和数据库就一样了,对于这个就 要转码:insert into user(loginName,password,name) values('admin','admin','管理员');这个时候“管理员”这个值就要转码,上面类方法Strto.toIso("管理员")在往 数据库中送,这个就可以存入
在取的时候,涉及到有中文的变量在页面上要转码为gb2312,方法Strto.toChinese(string srt);
以上方法很有局限性质,下面介绍一种更通用的方法:
1、页面编码 (采用:<%@ page contentType="text/html;charset=gb2312" language="java"%>)
2、传参编码 (采用过滤器)
3、 jdbc URL 编码 (采用:conn=DriverManager.getConnection("jdbc:mysql://localhost: 3306/project1?useUnicode=true&characterEncoding=GBK","admin","adminisme");
4、数据库编码 (无须修改mysql的文件,最后在不修改情况下来实现)
具体方法:
一、页面编码如上采用:<%@ page contentType="text/html;charset=gb2312" language="java"%>
二、传参编码:写如下一个类
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class EncodingFilter implements Filter {
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException,
ServletException {
// Conditionally select and set the character encoding to be used
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null) {
request.setCharacterEncoding(encoding);
}
}
// Pass control on to the next filter
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null) {
this.ignore = true;
} else if (value.equalsIgnoreCase("true")) {
this.ignore = true;
} else if (value.equalsIgnoreCase("yes")) {
this.ignore = true;
} else {
this.ignore = false;
}
}
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
并且在WEB-INFO的文件下的web.xml中加入
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>…….EncodingFilter</filter-class> //省略号省略的是这个类所在的包
- <init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
- <filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
三、 jdbc URL 编码 采用上面的:conn=DriverManager.getConnection("jdbc:mysql://localhost: 3306/project1?useUnicode=true&characterEncoding=GBK","admin","adminisme");
四、数据库编码
我们这里采用的在家里表的时候设置编码来解决具体如下实例
create table user (id bigint not null auto_increment,
loginName varchar(32) not null unique key,
password varchar(32),
userName varchar(32) not null,
email varchar(128),
createTime datetime,
modifyTime datetime,
primary key (id))DEFAULT CHARSET=gbk;
关键是红色字体部分,这样就保证了页面-参数-url-表的编码统一,不管数据本身是不是GBK的,但表是GBK的就可以了,(gb2312是GBK的子集)