之前不管怎么该JSP和servlet的编码格式,从网页的表单传入中文的时候,录入到数据库都会乱码,变成?????
之后发现,不是网页编码格式的问题,要在数据源那里,connection的时候在URL后面增加?characterEncoding=utf8才能解决问题
原来的是
public Connection getConnection(){
Connection conn = null;
try{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/mytest";
String username="root";
String password="";
conn = DriverManager.getConnection(url, username, password);
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}
return conn;
}
在URL后面加上编码格式之后是
public Connection getConnection(){
Connection conn = null;
try{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/mytest?characterEncoding=utf8";
String username="root";
String password="";
conn = DriverManager.getConnection(url, username, password);
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}
return conn;
}
就解决问题了,中文正常了!