Java; JSP; Tomcat; MySQL; hibernate; comprehensive solution for encoding Chinese garbled characters

Source: Internet
Author: User
Tags mysql command line

First, you can set encoding in those places in Java.
There are a lot of places in the development tool to set encoding, which is not difficult to understand. I will not introduce it here.

The following two encoding formats apply to JSP pages (*. jsp)
<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "UTF-8" %>
<% @ Page contenttype = "text/html; charset = UTF-8" %>

The following method is applicable to JSP, Servlet, and Action (*. Java)
Request. setcharacterencoding ("UTF-8 ");
Response. setcharacterencoding ("UTF-8 ");

The following is a suitable HTML page (*. HTM; *. html)
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">

Encode ate (server. XML)
<Connector other omitted Port = "80" uriencoding = "UTF-8">

MySQL encoding command

Set character_set_client = utf8;
Set character_set_connection = utf8;
Set character_set_database = utf8;
Set character_set_results = utf8;/* be sure to use it here */
Set character_set_server = utf8;

Set collation_connection = utf8_bin;
Set collation_database = utf8_bin;
Set collation_server = utf8_bin;

Configure the default encoding in my. ini
Default-character-set = utf8

Connect to the database and set the Encoding
JDBC: MySQL :/// 192.168.0.5: 3306/test? Characterencoding = utf8

/*************************************** ** Java and mysq encoding correspond ********************************* *******/

Common Coding UTF-8 in Java; GBK; gb2312; ISO-8859-1;
Corresponds to the UTF-8, GBK, gb2312, and Latin1 codes in the MySQL database.

/*************************************** ********************************* ************/

// Setcharacterencodingfilter. Java)
Package com. SORC;

Import java. Io .*;
Import javax. servlet .*;
Import javax. servlet. http .*;

Public class setcharacterencodingfilter extends httpservlet implements filter {
Private filterconfig;
Private string encoding = NULL;
// Handle the passed-In filterconfig
Public void Init (filterconfig ){
This. filterconfig = filterconfig;
Encoding = filterconfig. getinitparameter ("encoding ");
}
// Process the request/response pair
Public void dofilter (servletrequest request, servletresponse response, filterchain ){

Try {
Request. setcharacterencoding (encoding );
Filterchain. dofilter (request, response );
} Catch (servletexception SX ){
Filterconfig. getservletcontext (). Log (sx. getmessage ());
} Catch (ioexception iox ){
Filterconfig. getservletcontext (). Log (iox. getmessage ());
}
}
// Clean up resources
Public void destroy (){
}
}
// Web. xml configuration filter method (Web. xmd)
<Filter>
<Filter-Name> setcharacterencodingfilter </filter-Name>
<Filter-class> com. SORC. setcharacterencodingfilter </filter-class>
<Init-param>
<Param-Name> encoding </param-Name>
<Param-value> utf8 </param-value>
</Init-param>
</Filter>
<Filter-mapping>
<Filter-Name> setcharacterencodingfilter </filter-Name>
<URL-pattern>/* </url-pattern>
</Filter-mapping>

/*********************** With the above basic solution for the interview ***** ************************************/

1. Solutions Using GBK Encoding
The simplest way to set the encoding is to use the GBK database GBK and then use a filter to filter and encode it into GBK.
Effect: add data without garbled characters read data without garbled characters database management tool without garbled code everywhere SQL structure and data without garbled characters

2. coding solutions with UTF-8
All encodings are set to UTF-8
Database code utf8
Set the filter encoding utf8
Database connection? Characterencoding = utf8
Then run set character_set_results = GBK in the database management tool or MySQL command line;
The result is that the added data has no garbled characters. The database management tool has no garbled characters. The SQL structure and data are garbled.

3. Solutions for using the utf8 database to use Latin1 on the page
Jap Java Tomcat set to UTF-8
Filter utf8
Database connection? Characterencoding = Latin1
Other database Latin1
Then run set character_set_results = GBK in the database management tool or MySQL command line;
The result is that the added data has no garbled characters. The database management tool has no garbled characters. The SQL structure and data are garbled.

This should be written in hibernate. cfg. xml.

<Property
Name = "connection. url"> JDBC: mysql: // localhost: 3306/yidishui? Useunicode = true & amp; characterencoding = UTF-8 </property>

If hibernate. properties is used
# Hibernate. Connection. url JDBC: mysql: // localhost: 3306/test? Useunicode = true & characterencoding = UTF-8

2. Modify the my. ini configuration file of the MySQL database

[Client]

Port = 3306

Default-character-set = GBK

# Here, the default encoding is changed to GBK or UTF-8.

3. When creating a data table

Create Table 'Article '(

'Article _ id' varchar (100) not null default '0 ',

'Article _ title' varchar (100) default null,

Primary Key ('Article _ id ')

) Engine = MyISAM default charset = GBK;

GBK encoding is used here.

4. Use UTF-8 encoding in JSP pages

5. Add a filter.

6. Add character set utf8 collate utf8_unicode_ci after the table field, for example:
Username varchar (20) Character Set utf8 collate utf8_unicode_ci not null,

The complete method is:
Drop table if exists blog;
Create Table blog (
Id integer primary key,
Username varchar (20) Character Set utf8 collate utf8_unicode_ci not null,
Password varchar (20) Character Set utf8 collate utf8_unicode_ci not null,
Subject varchar (100) Character Set utf8 collate utf8_unicode_ci not null,
Email varchar (50) Character Set utf8 collate utf8_unicode_ci not null,
Image varchar (100) Character Set utf8 collate utf8_unicode_ci null,
Visitcount integer null
);

7. MySQL status command:

If the client characterset is not GBK or utf8, it may be garbled during data insertion. Solution: use MySQL to set software settings and use command lines manually. This may be because of command errors, it won't work if it's done.

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.