The examples in this article share with you the problem of garbled java Chinese value transmission and solutions for your reference. The specific content is as follows
General encoding format settings:
1. It can be encoded twice, that is, after setting the character set, decoding the character set before insertion, which is also the most effective way.
Set character set:
String value=null;
try { value= URLEncoder.encode(jsonObjectPar.getString("value"), "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); }
Decoded 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 in Chinese when it is inserted, and the code is garbled after entering the database, the database is generally not set to UTF-8;
First check the database encoding (MySQL as an example):
Run in database query: show variables like 'character%'; view all encoding information modify the character set that is not UTF-8, mainly check whether the datebase is UTF-8, the 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 can see that the encoding of the database is latin1 and needs to be modified to gbk or utf8;
Among them, character_set_client is the client encoding method; character_set_connection is the encoding used to establish a connection; character_set_database database encoding; character_set_results result set encoding; character_set_server database server encoding. As long as the above four encoding methods are ensured that the above four are the same, there will be no garbled problems.
4. The problem of garbled code from the background to the page:
Set the character set before returning data:
response.setCharacterEncoding("utf-8");
5.jsp page to the background garbled code:
5-1.jsp's encoding is set to utf-8
5-2. This is the judgment in the background
String name = request.getParameter("ABC"); if(name.equals(new String(request.getParameter("ABC").getBytes("iso8859-1"), "iso8859-1"))) { name = new String(name.getBytes("iso8859-1"),"UTF-8"); }The code submitted by get method iso8859-1
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.