Encoding discussion-the ultimate solution to Garbled text

Source: Internet
Author: User

Many people, especially new users, are working on J2EE projects.
Always suffering from coding problems like this or that

Here we will discuss how to learn from new users.
(Using Tomcat MYSQL as an example, I recommend that all codes use UTF-8)

1 Project
All. java. jsp. xml. txt files in the project have the default encoding. The default encoding is the system environment encoding.
In our Chinese system, UTF-8 is recommended for GBK.
When UTF-8 is used, you may encounter garbled characters when compiling and generating DOC files (especially when ant is used, you may encounter almost 100% of the generated DOC files)

The solution uses ant as an example.
Note the encoding parameter for compilation
<Target name = "build">
<Mkdir dir = "$ {build. dir}"/>
<Javac encoding = "UTF-8" destdir = "$ {build. dir} "target =" 1.3 "DEBUG =" true "deprecation =" false "optimize =" false "failonerror =" true ">
<SRC Path = "$ {SRC. dir}"/>
<Classpath refID = "Master-classpath"/>
</Javac>
</Target>

Generate Doc note encoding and charset
<Target name = "Doc">
<Mkdir dir = "Doc"/>
<Javadoc charset = "UTF-8" encoding = "UTF-8" packagenames = "$ {packages}" sourcepath = "src" destdir = "Doc" author = "true" version = "true" use = "true" splitindex = "true">
<Classpath refID = "Master-classpath"/>
</Javadoc>
</Target>

Here, encoding refers to the Java file encoding formats javac and javadoc both have this parameter.
Charset refers to the javadoc parameter of the generated Doc encoding method.

2 Databases
MySQL encoding is the most complex. Since MySQL 4, MySQL claims to support multi-encoding. It is more flexible and more troublesome.
MySQL has four levels of encoding.
System Level
Database-level
Table-level
SQL statement level
We recommend that you use UTF-8 for unified encoding.
Other databases are generally encoded in a simple way.

3. Web Server
Tomcat as an Example
A parameter in Tomcat server. xml
<Connectorport = "8080" maxhttpheadersize = "8192"
Maxthreads = "150" minsparethreads = "25" maxsparethreads = "75"
Enablelookups = "false" redirectport = "8443" acceptcount = "100"
Connectiontimeout = "20000" disableuploadtimeout = "true" urincoding = "UTF-8"/>
The urincoding parameter is mainly encoded in the get method.

4. jsp display Layer
The format of the JSP file is described in Article 1st.
Many friends use eclipse + myeclipse to generate JSP
It automatically generates a header <% @ page Language = "Java" Import = "Java. util. *" pageencoding = "UTF-8" %>
Do not misunderstand this sentence. It cannot be guaranteed that what you see in IE is not garbled.
Pageencoding it means that the page uses UTF-8 (it seems to be effective only in eclipse, I'm not sure)
To avoid garbled characters in IE, you have to add a sentence <% @ page contenttype = "text/html; charset = UTF-8" %>
It cannot be automatically generated in (myeclispe). We recommend that you modify the myeclipse template in the following directory.
Myeclipse/Eclipse/plugins/COM. genuitec. Eclipse. wizards_4.0.1/templates
<% @ Page contenttype = "text/html; charset =$ {encoding}" %>

5 Filter
Since Tomcat 4 and later, A setcharacterencodingfilter has been circulating on the Internet.
It is easy to add in Web. xml
<Filter>
<Filter-Name> set character encoding </filter-Name>
<Filter-class> filters. setcharacterencodingfilter </filter-class>
<Init-param>
<Param-Name> encoding </param-Name>
<Param-value> UTF-8 </param-value>
</Init-param>
</Filter>
<Filter-mapping>
<Filter-Name> set character encoding </filter-Name>
<URL-pattern>/* </url-pattern>
</Filter-mapping>

6. resource files
First, ensure that the file itself is UTF-8
Then native2ascii conversion is used during deployment.
Here is an example in ant.
<Native2ascii encoding = "UTF-8" DEST = "$ {web. dir}/WEB-INF/classes "src =" $ {SRC. dir} "des = "**/*. properties "/>

Summary
Here you should have completely fixed the Garbled text.
The writing is a little messy. Please give more advice to the experts.

(Author: easyjf open-source team. Thank you for your reprinting !)

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.