1.把项目设置为utf-8
2.jsp页面上的编方式统一改为"UTF-8" //必做
3.Servlet中doGet/doPost方法的开始代码写上
req.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=UTF-8");
4.对于get方式提交到Servlet的数据,中文乱码处理请参照下面示例
String username = request.getParameter("username");
username = new String (username.getBytes("ISO-8859-1"),"UTF-8");
5. MySql的jdbc连接后加上?useUnicode=true&characterEncoding=UTF-8
其它数据库(oracle, sqlServer, db2)不需要这一步。
示例:
"jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8";
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/fs?useUnicode=true&characterEncoding=UTF-8", "root", "123");
//如果通过上面几步后还有乱码,则追加下面一步
6. 在tomact的安装目录的/conf/server.xml文件中找到Connector,加上URIEncoding="utf-8",
<Connector
port="8080"
maxHttpHeaderSize="8192"
maxThreads="150"
minSpareThreads="25"
maxSpareThreads="75"
enableLookups="false"
redirectPort="8443"
acceptCount="100"
connectionTimeout="20000"
disableUploadTimeout="true"
URIEncoding="utf-8"/>