Generation and solution of MySQL + tomcat garbled characters

Source: Internet
Author: User

The following articles mainly introduce the actual solution for garbled MySQL + tomcat, the reasons for garbled MySQL databases, and the specific solutions, at the same time, we also have an introduction to the garbled solution under tomcat.

I. MySQL-caused garbled Solutions

It is generally caused by the following reasons:

(1) Chinese characters are processed normally in java. Displaying garbled characters in the cmd client is a problem with character sets.

(2) The length of the field is long enough. However, when a Chinese character is inserted, the system prompts com. MySQL. jdbc. MySQLDataTruncation: Data truncation: Data too long for column.

(3) garbled characters are the final issue of character sets. We should consider the following aspects from Character Set settings: server, client, database, connection, and results.

Solution

(1) modify the my. ini (MySQL Server Instance Configuration file)

 
 
  1. # CLIENT SECTION  
  2. [client]  
  3. port=3306 
  4. [MySQL]  
  5. default-character-set=utf-8  
  6. # SERVER SECTION  
  7. [MySQLd]  
  8. default-character-set=utf-8  

(2) modify the db. opt configuration file under the corresponding database directory in the data Directory

 
 
  1. default-character-set=utf-8   
  2. default-collation=utf8_general_ci 

(3) Character Set specified in the database connection string

 
 
  1. URL=jdbc:MySQL://yourIP/college?user=root&password=yourPassword&useUnicode=true&characterEncoding=utf-8 

(4) Specify the character set when creating a database

 
 
  1. create database yourDB CHARACTER SET utf-8; 

OK. After the above four settings, the garbled problem caused by MySQL has been solved!

Ii. garbled solution under tomcat

First, the garbled problem is divided into three types of JSP pages to display Chinese garbled characters; Form submission garbled; database application garbled characters

1. garbled characters appear when the JSP page outputs Chinese Characters

Solution: Use the page command in the JSP file to specify the MIME type of the response result, for example, <% @ page language = "java" contentType = "text/html; charset = UTF-8" %>

2. Form submission garbled (two submission methods are available: post and get)

The request. getParameter method is used to get garbled characters, because the default iso-8859-1 is used when tomcat processes submitted parameters, and the problem of Form submission get and post processing garbled characters is different.

(1) POST Processing Method

You can write a filter to resolve the form submitted by post. The filter is called before the data submitted by the user is processed. You can change the parameter encoding method here,

The filter code is as follows:

 
 
  1. package com.webim.search;  
  2. import javax.servlet.*;   
  3. import javax.servlet.http.HttpServletRequest;   
  4. import javax.servlet.http.HttpServletResponse;  
  5. public class FileFilter implements Filter {  
  6. protected String encoding = null;   
  7. protected FilterConfig filterConfig = null;   
  8. protected boolean ignore = true;  
  9. public void init(FilterConfig filterConfig)   
  10. throws javax.servlet.ServletException {   
  11. this.filterConfig = filterConfig;   
  12. this.encoding = filterConfig.getInitParameter("encoding");   
  13. String value = filterConfig.getInitParameter("ignore");   
  14. if (value == null) {  
  15. this.ignore = true;  
  16. } else if (value.equalsIgnoreCase("true")) {  
  17. this.ignore = true;  
  18. } else if (value.equalsIgnoreCase("yes")) {  
  19. this.ignore = true;  
  20. } else {  
  21. this.ignore = false;  
  22. }  
  23. }  
  24. public void doFilter(ServletRequest request, ServletResponse response,   
  25. FilterChain chain) throws java.io.IOException,   
  26. javax.servlet.ServletException {  
  27. if (ignore || (request.getCharacterEncoding() == null)) {  
  28. String encoding = selectEncoding(request);  
  29. if (encoding != null) {  
  30. request.setCharacterEncoding(encoding);  
  31. }  
  32. }   
  33. chain.doFilter(request, response);  
  34. }  
  35. public void destroy() {   
  36. this.encoding = null;   
  37. this.filterConfig = null;  
  38. }  
  39. protected String selectEncoding(ServletRequest request) {  
  40. return (this.encoding);  
  41. }  
  42. }  

Add a filter to the web. xml file:

 
 
  1. <Filter>
  2. <Filter-name> FileFilter </filter-name>
  3. <Filter-class> com. webim. search. FileFilter </filter-class>
  4. <Init-param>
  5. <Param-name> encoding </param-name>
  6. <Param-value> UTF-8 </param-value>
  7. <! -- Gbk, gb2312 or UTF-8 -->
  8. </Init-param>
  9. <Init-param>
  10. <Param-name> ignore </param-name>
  11. <Param-value> true </param-value>
  12. </Init-param>
  13. </Filter>
  14. <Filter-mapping>
  15. <Filter-name> FileFilter </filter-name>
  16. <Servlet-name>/* </servlet-name>
  17. </Filter-mapping>

* Note that the filter element should be placed before all web. xml elements.

(2) Get Method Processing

Tomcat has different processing methods for post and get, so the filter cannot solve the get garbled problem. It needs to be set elsewhere.

Open the server. xml file in the <tomcat_home> \ conf directory, find the Settings section of the Connector component for the service on port 8080, and add a property for this component: URIEncoding = "UTF-8 ". The changed ctor settings are as follows:

 
 
  1. <Connector port="8080" maxHttpHeaderSize="8192" 
  2. maxThreads="150" minSpareThreads="25" maxSpareThreads="75" 
  3. enableLookups="false" redirectPort="8443" acceptCount="100" 
  4. connectionTimeout="20000" useBodyEncodingForURI="true" URIEncoding="utf-8" disableUploadTimeout="true" /> 

* After modification, restart tomcat.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.