Solutions to Java Chinese garbled characters

Source: Internet
Author: User

Sun Xin's original series-solutions to Java Chinese garbled characters

[Keywords: Sun Xin Java garbled]

Solutions to Java Chinese garbled characters

As long as you have mastered the causes of Chinese Garbled text, and then remedy the problem, you can solve these problems smoothly. Next we will analyze the scenarios that are prone to garbled characters and propose solutions.1. The form data submitted using the POST method contains Chinese CharactersSince the Web Container's default encoding method is ISO-8859-1, in Servlet/jsp programs, the string obtained through the getparameter () method of the request object is converted to the ISO-8859-1, this is one of the reasons for Garbled text. To prevent the container from returning strings encoded in ISO-8859-1, for form data submitted using the POST method, you can call request before obtaining the request parameter value. setcharacterencoding ("GBK"), specify that the character encoding method used by the request body is GBK. Before sending Chinese data to the browser, call response. setcontenttype ("text/html; charset = GBK") to specify the encoding method of the output content as GBK. For JSP pages, before obtaining the request parameter value, write the following code: <% request. setcharacterencoding ("gb2312"); %> to specify the encoding format of the output content, set the page command contenttype attribute as follows: <% @ page contenttype = "text/html; charset = GBK "%> the following code is automatically added to the servlet class after the Web Container converts the JSP page: response. setcontenttype ("text/html; charset = GBK ");2. The form data submitted using the get method contains Chinese CharactersWhen the form is submitted using the get method, the submitted data is appended to the end of the URL as the query string and sent to the server. At this time, the server does not have a function to call the setcharacterencoding () method. We need to perform correct encoding conversion after obtaining the value of the request parameter. String name = request. getparameter ("name"); name = new string (name. getbytes ("ISO-8859-1"), "GBK"); In the first line, the Unicode value of the string name obtained by calling the getparameter () method is converted to ISO-8859-1 encoding and called name. getbytes ("ISO-8859-1") gets the original GBK encoding value, and then the call to new string () reconstructs the Unicode encoding of the string in the GBK character set. To facilitate the conversion from ISO-8859-1 encoding to GBK, we can write a tool method, as follows: Public String togbk (string Str) throws Java. io. unsupportedencodingexception {return new string (Str. getbytes ("ISO-8859-1"), "GBK ");}3. Store and read Chinese data in the databaseFor the JDBC driver of most databases, the data transmitted between the Java program and the database is in the ISO-8859-1 as the default encoding format, so when we store data containing Chinese to the database in the program, the JDBC driver first converts the data in the Unicode encoding format inside the program to the ISO-8859-1 encoding, and then passes it to the database, plus the database itself also has a character set, this is why we often read garbled characters when reading Chinese data in the database. To solve the preceding problem, you only need to change the default encoding format of the database to GBK or gb2312. Different databases also provide other methods to handle character encoding conversion, in the actual application process, you can perform specific processing based on the specific situation. As long as you understand the encoding conversion process, you can find the problem and solve the problem.4. servlet/jsp running on platforms of different language systemsSometimes, when a web application developed on the Chinese system platform is transplanted to the English system platform, Chinese strings directly written in Servlet and JSP are displayed as garbled characters in the output. This is because when compiling Servlet classes or JSP files, if the-encoding parameter is not used to specify the encoding format of the Java source program, javac will obtain the default character set used by the local operating system, using this character set, the Java source program is converted to unicode encoding and saved to the memory. Then, the source program is compiled into a bytecode file (the bytecode file adopts UTF-8 encoding) and saved to the hard disk. In the English platform, the default encoding format is ISO-8859-1, so after compilation and conversion, the execution of the output, the original source file written in the Chinese string becomes garbled. To solve this problem, you can use the-encoding parameter to specify the Encoding As GBK or gb2312 when compiling the source program of the servlet class, for example, javac-encoding GBK helloservlet. java for JSP pages, as long as the contenttype attribute or pageencoding attribute is used in the page command to specify the encoding format as GBK or gb2312, the Web Container can correctly convert and compile the JSP file. For example: <% @ page contenttype = "text/html; charset = GBK" %> or <% @ page pageencoding = "GBK" %> in actual Web applications, garbled characters are generated for a variety of reasons. However, as long as we understand the conversion process of character encoding, carefully analyze the causes of garbled characters, and find the key to the problem, we can remedy the problem and solve the problem. + ------------------------- Save it to your blog. I am always confused about Chinese garbled characters.
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.