Solve the Problem of garbled Chinese Characters in reading MySQL Data stored in Jsp in Java Web Development

Source: Internet
Author: User
MySQL is easy to use, but it is prone to Chinese garbled characters. The following lists several common Chinese garbled characters in Java Web development: 1. JSP page display problems 2. Number of MySQL stored

MySQL is easy to use, but it is prone to Chinese garbled characters. The following lists several common Chinese garbled characters in Java Web development: 1. JSP page display problems 2. Number of MySQL stored

Although MySQL is easy to use, it is prone to Chinese garbled characters. The following lists several common Chinese garbled characters in Java Web development:

1. JSP page display problems

2. garbled Chinese characters stored in MySQL DATA

3. Chinese garbled characters for reading MySQL Data

Character Set: A character set defines the characters it contains and the numbers corresponding to each character.
Character encoding: Stores numbers in a computer and converts them to corresponding characters when displayed. Encoding means the storage of character numbers.

The introduction is too embarrassing. If you do not engage in this study, you can ignore it completely. As long as you know that UTF-8 is the product of all character encoding for the unified world, OK.

The solution is king:

(If the CKEditor plug-in is used on the page, Chinese garbled characters do not have much to do with it)

1. Chinese garbled characters are displayed in JSP. Generally, page encoding is not set. Add the following code.

<% @ Page pageEncoding = "UTF-8" %>

<% @ Page language = "java" contentType = "text/html; charset = UTF-8" %>

"PageEncoding" specifies the encoding method used by the current jsp file storage. "contentType" is used to specify "Content-Type" in the Response Header message"

When charset is not specified in the response header of the server, the client decodes the Page Based on the encoding method specified by charset in the above Code.

And JSP are stored in UTF-8 encoding, all possible problems through the erased. If you use Myeclipse, you can set the default parameters of Myeclipse, so that Myeclipse new JSP file, can be automatically used to encode the UTF-8. Method: Windows-Preference-Myeclipse-File and Editor-JSP-Encoding, select ISO 10646/Unicode (UTF-8)

2. Encoding settings for data storage and MySQL tables. It is best to specify the corresponding encoding format. Select utf8 as the character set to verify utf8_unicode_ci.

When installing MySQL, you will be prompted about the default encoding. You can also set it by yourself.

Modify my. ini (files in the MySQ installation directory)

# CLIENT SECTION
[Client]
Port = 3306
[Mysql]
Default-character-set =Utf8

# The default character set that will be used when a new schema or table is
# Created and no character set is defined
Default-character-set =Utf8

After modification, remember to restart MySQL !!!Modifying this file is because I set CharacterEncoding, specified the page encoding, and used the filter, it still cannot solve the problem. When it almost crashes, the final life-saving straw!

3. When reading data, add request. setCharacterEncoding ("UTF-8") in Servlet; can solve Chinese parameter garbled from JSP page.

4. Use a filter. If the default MySQL encoding format is UTF-8, you can solve all the problems by using only the filter, no matter whether you have set the complicated encoding.

Create a class: CharacterEncodingFilter in the package

Package com. rady. blog;

Import java. io. IOException;

Import javax. servlet. Filter;
Import javax. servlet. FilterChain;
Import javax. servlet. FilterConfig;
Import javax. servlet. ServletException;
Import javax. servlet. ServletRequest;
Import javax. servlet. ServletResponse;

Public class CharacterEncodingFilter implements Filter {
Private FilterConfig config;
Private String encoding = "UTF-8 ";

Public void destroy (){
Config = null;
}

@ Override
Public void doFilter (ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
Request. setCharacterEncoding (encoding );
Chain. doFilter (request, response );
}

Public void init (FilterConfig config) throws ServletException {
This. config = config;
String s = config. getInitParameter ("encoding ");
If (s! = Null ){
Encoding = s;
}
}
}

Add in web. xml

EncodingFilter
Com. rady. blog. CharacterEncodingFilter

Encoding
UTF-8



EncodingFilter
/*

Finally, we also killed Tomcat to prevent problems before they even occurred. The Chinese language is no longer available!

Modify the Tomcat installation directory/conf/server. xml file

ConnectionTimeout = "20000"
RedirectPort = "8443" URIEncoding =" UTF-8"/>

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.