I. Program description
1. Database tutorial: mysql tutorial
2. Development environment: tomcat7.0 + windows7 + myeclips tutorial e9.0
3. Solve the problem: solve the problem of Chinese garbled characters in the jsp tutorial.
II. Basic process
III. Running interface
1. User registration interface
2. JQuery form verification
3. Submit post information
4. Database information
4. Main code of the program
1. Character filter (CharacterFilter. java)
1 public class CharacterFilter implements Filter {
2 String encoding = null; // declare the character encoding
3 @ Override
4 public void destroy (){
5 encoding = null; // when the filter object is destroyed, the character encoding value is also changed to null
6}
7 @ Override
8 public void doFilter (ServletRequest request, ServletResponse response,
9 FilterChain chain) throws IOException, ServletException {
10 if (encoding! = Null ){
11 request. setCharacterEncoding (encoding); // you can specify the encoding format of the request.
12 response. setContentType ("text/html; charset =" + encoding); // Set the response Character encoding
13}
14 chain. doFilter (request, response );
15}
16 @ Override
17 public void init (FilterConfig filterConfig) throws ServletException {
18 encoding = filterConfig. getInitParameter ("encoding"); // Get the initialization parameter
19}
20}
2. web. xml configuration
1 <filter>
2 <filter-name> CharacterFilter </filter-name>
3 <filter-class> com. swyma. md5.filter. CharacterFilter </filter-class>
4 <init-param>
5 <param-name> encoding </param-name>
6 <param-value> UTF-8 </param-value>
7 </init-param>
8 </filter>
9 <filter-mapping>
10 <filter-name> CharacterFilter </filter-name>
11 <url-pattern>/* </url-pattern>
12 </filter-mapping>
3. Database connection class (please pay special attention to the url)
1 public class DBCon {
2 private static Connection conn = null;
3 public static Connection getConn (){
4 try {
5 Class. forName ("com. mysql. jdbc. Driver"); // load the database connection Driver
6 String user = "root"; // user name
7 String pwd = "123"; // password
8 String url = "jdbc: mysql: // localhost: 3306/md5? UseUnicode = true & characterEncoding = utf-8 "; // database connection url
9 conn = DriverManager. getConnection (url, user, pwd); // get the connection
10} catch (Exception e ){
11 e. printStackTrace ();
12}
13 return conn;
14}
15}
4. Database mysql
Drop table if exists 'md5'. 'TB _ user ';
Create table 'md5'. 'TB _ user '(
'Id' int (11) not null AUTO_INCREMENT,
'Name' varchar (20) not null,
'Pwd' varchar (40) not null,
'Sex 'varchar (5) not null,
'Age' int (11) not null,
'Createtime' datetime not null,
Primary key ('id ')
) ENGINE = InnoDB default charset = utf8;
V. Personal summary
1. First of all, let me explain some of my feelings:
In fact, this Chinese garbled problem was solved as early as last summer when I learned jsp, but the program did not know anything to delete it because it was not summarized in time. What is the result, it took me more than a day to complete it.
The price is too bad, so the summary is very important. If you see this, please start to summarize it.
2. This is a simple jsp to solve Chinese garbled characters.
3. Solution Key: first, CharacterFilter. java, character filter, which unified the jsp page encoding into UTF-8;
Second, in DBCon. java, the url useUnicode = true & characterEncoding = UTF-8 must be added. Otherwise, garbled characters will also occur.
4. Solution process:
1) at first, I added a character filter and thought it would work. No matter how I debug it, the results are still garbled. Then I found some materials on the Internet, and I feel that characterFilter is not wrong, but it is a matter of hope.
No, you have to debug it step by step.
2) it may be because my search capability is not strong. I checked a lot of information, but I can add a CharacterFilter, but this system does not work.
3) in the end, let's rely on our own memories. I think of this url, so I am lucky to solve the problem.
5. The problem is solved, but I still feel that it is not perfect, because I sometimes encounter the java. lang. NoClassDefFoundError: Filter error during deployment, and I feel strange. If you have the same problem or a good solution as you have, I hope to provide some guidance.