1. Chinese back to front page will appear garbled
If the above statement is set only in the servlet, the following occurs (dynamic content is garbled, static content is normal)
Req.setcharacterencoding ("Utf-8");
So the code is also specified on the foreground page.
<%@ page contenttype= "Text/html;charset=utf-8" language= "java"%>
So that the front page can show normal
2. Database Query Chinese garbled
Database connection mode
Private final String Conn_url = "Jdbc:mysql://localhost:3306/weixinmybatis"; Class.forName ("Com.mysql.jdbc.Driver"); Connection conn = drivermanager.getconnection (Conn_url, "root", "root");
When using PreparedStatement's setstring (int, string) method, if the string is Chinese, it becomes. , causing the query to fail
StringBuilder sql = new StringBuilder ("Select Id,command,description,content from MESSAGE where 1=1"); Sql.append ("and Command=? "); PreparedStatement PS = conn.preparestatement (sql.tostring ());p s.setstring (1, "Chinese");
The resulting query statement becomes the
Select Id,command,description,content from MESSAGE where 1=1 and command= '?? ‘
The final solution is to set the encoding when connecting to the database, that is, adding the encoding in the Conn_url
Private final String Conn_url = "Jdbc:mysql://localhost:3306/weixinmybatis?characterencoding=utf8";
Note: Second lesson in learning javaweb development