A solution to the Java garbled Problem

Source: Internet
Author: User

1. Problem Description
I have developed a Java Web project under Windows, because I am not familiar with Java, and the description here is a layman. Here is my solution to Chinese Garbled text.

2. Method

In Windows, IDE uses the system character set, so it is GBK. From Page encoding to database, to post data, it is GBK. In the win environment, there is indeed no problem, however, the server is a Linux environment, so there is a porting problem.
After I set up the environment in Linux, the page shows no problem in English. The main reason is that after the data is submitted, it is garbled. After checking, the data written into the database is already garbled, this is why the encoding of the page is submitted. I use
Response. setcontenttype ("text/html; charset = GBK ");
Request. setcharacterencoding ("GBK ");
After the preceding statement is added, it does not work. String title = new string (request. getparameter ("title "). getbytes ("ISO-8859-1"), "GBK ");
Statement. This statement is normal under Win.
After Google review, I found a prompt to modify the server. xml file in Tomcat.

<Connector Port = "8080" protocol = "HTTP/1.1" connectiontimeout = "20000" uriencoding = "GBK" redirectport = "8443"/>

Change the default UTF-8 to GBK encoding, and then get the parameter directly from the text box, that is, the string Title = new string (request. getparameter ("title "). getbytes ("ISO-8859-1"), "GBK"); statement modified to String title = request. getparemeter ("titile"); restarted tomcat !!
 
The above problem is solved, but garbled characters are displayed in HTML, and the JSON encoding problem can be found. The JSON file problem needs to be solved in this project.ProgramWrite a JSON file, and introduce it through the <SCRIPT> statement in HTML. The file encoding generated in Linux uses the UTF-8 format by default. We can view the file encoding through the file command, the page is GBK encoded, so garbled characters are generated. in <SCRIPT>, set the charset attribute: <SCRIPT charset = "UTF-8"> in this way, the Chinese characters in the JSON file can be correctly read, the remaining explanation is handed over to the browser.

This was successfully tested in my Linux environment, but the formal production server still had some problems. After testing, the encoding was still messy, but I have modified the configuration according to the above? I added
Response. setcontenttype ("text/html; charset = GBK ");
Request. setcharacterencoding ("GBK ");
The two statements have succeeded again .....

I am not clear about the specific reasons. I will record them here first.

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.