Java Chinese garbled solution (4) ----- java coding conversion process, java ----- java

Source: Internet
Author: User
Tags java format

Java Chinese garbled solution (4) ----- java coding conversion process, java ----- java

The first three blogs focus on character encoding. Through these three blogs, you will have a preliminary understanding of various character encoding methods, to understand the Chinese language of java, you must understand it. However, understanding this is just the beginning. The following blog will focus on how java Garbled text is generated, what Garbled text exists, and how to fundamentally solve the Garbled text problem. You can conquer annoying java garbled issues with bloggers !!!


Java coding and conversion process

We always use a java file to perform the most direct interaction (input and output) with users. The interaction content may contain Chinese characters. Whether these java classes interact with databases or front-end pages, their lifecycles are always like this:

1. programmers write program code in the editor on the operating system and save the operating system in. java format. These files are called source files.

2. compile these source files through javac.exe in JDK to form a. class.

3. Run these classes directly or deploy them in a WEB container to obtain the output result.

These processes are observed from a macro perspective. It is definitely not feasible to understand this process. We need to really understand how java is encoded and decoded:

Step 1:When we use an editor to write a java source file, the program file will be saved in the default encoding format of the Operating System (generally, the Chinese operating system uses the GBK encoding format) to form. java file. Java source files are saved in the file. encoding format supported by the operating system by default. The following code shows the system's file. encoding parameter value.

System.out.println(System.getProperty("file.encoding"));

Step 2:When we use javac.exe to compile our java file, JDK will first confirm its compilation parameter encoding to determine the source code character set. If we do not specify this compilation parameter, JDK will first obtain the default file of the operating system. encoding parameter, then JDK will compile the java source program from file. the encoding format is converted to the JAVA internal default UNICODE format and placed into the memory.

Step 3:JDK writes the compiled information in the memory to the class file to form a. class file. At this time, the. class file is Unicode encoded. That is to say, the content of common. class files, whether Chinese or English characters, has been converted to the Unicode encoding format.

In this step, the JSP source file is handled in a different way: the WEB Container calls the JSP compiler. the JSP compiler first checks whether the JSP file has the file encoding format, if this parameter is not set, the JSP compiler calls JDK to convert the JSP file into a temporary servlet class using the default encoding method. class file and keep it in the Temporary Folder.

Step 4:Run the compiled class: There are several situations

1. Run the command directly on the console.

2. JSP/Servlet class.

3. java class and database.

The three cases are different in each case,

1. Classes running on the Console

In this case, the JVM first reads the class file stored in the operating system into the memory. In this case, the class file encoding format in the memory is Unicode, and then the JVM runs it. If you need to enter information, the file. encoding format is used to encode the information entered by the user and convert it to the Unicode encoding format and save it to the memory. After the program runs, the results are converted to file. encoding format and returned to the operating system and output to the interface. The process is as follows:

In the above process, no error occurs in any involved encoding conversion; otherwise, garbled characters are generated.

2. Servlet class

Since JSP files will eventually be converted to servlet files (but the storage location is different), we also include JSP files here.

When a user requests a Servlet, the WEB Container calls its JVM to run the Servlet. First, the JVM will load the servlet class to the memory. The servlet code in the memory is in the Unicode encoding format. Then, the JVM runs the Servlet in the memory. During the running process, if you need to accept data transmitted from the client (such as data transmitted through forms and URLs ), the WEB Container will accept the incoming data. During the receiving process, if the program sets the encoding of the incoming parameters, it adopts the set encoding format. If not, it adopts the default ISO-8859-1 encoding format, after receiving the data, the JVM will convert the data encoding format to Unicode and save it to the memory. The output result is generated after the Servlet is run, and the encoding format of the output result is Unicode. Then, the WEB Container sends the generated Unicode encoding string directly to the client. If the Program specifies the encoding format for output, the string is output to the browser according to the specified encoding format, otherwise, the default ISO-8859-1 encoding format is used. The process flow chart is as follows:

3. Database

We know that the connection between the java program and the database is connected through the JDBC driver, and the JDBC driver defaults to the ISO-8859-1 encoding format, that is to say, when we pass data to the database through the java program, JDBC first converts the data in the Unicode encoding format to the encoding format of the ISO-8859-1, and then stores the data in the database, that is, when the database saves the data, the default format is ISO-8859-1.

----- Original from: http://cmsblogs.com /? P = 1475Please respect the author's hard work and repost the source.

----- Personal site:Http://cmsblogs.com

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.