A solution to the problem of Java Chinese text-passing-value garbled _java

Source: Internet
Author: User

The example of this article for everyone to share the Java translation of the value of garbled problem, as well as the solution, for your reference, the specific contents are as follows

General encoding format settings:

1. Can be encoded after two times, that is, set the character set, decoding the character set before inserting, but also the most effective way
to set the character set:


try { value= urlencoder.encode (jsonobjectpar.getstring ("value"), "UTF-8"); Unsupportedencodingexception e) { e.printstacktrace (); }

Decode Character Set:

String Value=null; 
try { 
value= new String (Value.getbytes ("iso-8859-1"), "UTF-8"), 
catch (Unsupportedencodingexception e) { 
e.printstacktrace (); 
} 

2. Direct processing:

UserName = new String (username.getbytes ("iso-8859-1"), "UTF-8");

3. If the breakpoint is inserted when the time is Chinese, to the database after garbled, is generally the database is not set to UTF-8;

First look at the database encoding (for example, MySQL):
Run in database query: Show variables like ' character% '; View all encoded information
Modify the character set that is not UTF-8, mainly to see if Datebase is UTF-8, command

Show variables like ' character% '; 
+ ———————— –+ —————————-+ 
| variable_name | Value | 
+ ———————— –+ —————————-+ 
| character_set_client | latin1 | 
| character_set_connection | Latin1 | 
| Character_set_database | Latin1 | 
| Character_set_filesystem | binary | 
| Character_set_results | Latin1 | 
| Character_set_server | Latin1 | 
| Character_set_system | UTF8 | 
| Character_sets_dir | /usr/share/mysql/charsets/| 
+ ———————— –+ —————————-+ 

From the above information, we know that the encoding of the database is latin1, which needs to be modified as GBK or UTF8;
Among them, the character_set_client is the client encoding method, the encoding used for establishing the connection character_set_connection, the Character_set_database database encoding; character_set_ Results result set encoding, Character_set_server database server encoding, as long as the above four to ensure the same encoding, there will be no garbled problem.

4. From the background to the page garbled problem:
set the character set before returning data:
response.setcharacterencoding ("Utf-8");

5.jsp page to background garbled:
the 5-1.jsp encoding is set to Utf-8
5-2. Background judgment

String name = Request.getparameter ("ABC"); 
if (Name.equals (Request.getparameter ("ABC"). GetBytes ("Iso8859-1"), "iso8859-1")) { 
name = new String ( Name.getbytes ("Iso8859-1"), "UTF-8"); 
} 

The code submitted by get method is iso8859-1

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.