Classification of Chinese problems and recommended optimal solutions

Source: Internet
Author: User
Classification of Chinese problems and recommended optimal solutions
After learning about the above Java File Processing principles, we can propose a set of recommended methods to best solve the problem of Chinese characters.
Our goal is to compile the Java source program that contains Chinese strings or processes Chinese in the Chinese system and then move the value to any other operating system for proper operation, or, after compilation in other operating systems, it can run correctly, pass Chinese and English parameters correctly, and communicate with the database in Chinese and English strings correctly.
Our specific idea is to restrict the correct encoding method at the entry and exit of Java program transcoding and at the same time as the user's input/output conversion.
The specific solution is as follows:
1. For classes that run directly on the console
In this case, we recommend that you use the RST stream in the program to process the input and output if you want to receive the user's input or output that may contain Chinese characters from the client, specifically, the application is applicable to the following traffic types for ignore nodes:
File: filereader, filewrieter
The Byte node stream types are: fileinputstream and fileoutputstream.
Memory (array): chararrayreader, chararraywriter
Bytearrayinputstream and bytearrayoutputstream
Memory (string): stringreader, stringwriter
Pipeline: pipedreader and pipedwriter
The Byte node stream types are pipedinputstream and pipedoutputstream.
At the same time, you should use the following object-oriented stream for processing Input and Output:
Bufferedwriter, bufferedreader
The byte processing stream is bufferedinputestream and bufferedoutputstream.
Inputstreamreader, outputstreamwriter
The byte processing stream is datainputstream and dataoutputstream.
Inputstreamreader and inputstreamwriter are used to convert a byte stream to a bytes stream based on the specified character sequence set, for example:
Inputstreamreader in = new inputstreamreader (system. In, "gb2312 ");
Outputstreamwriter out = new outputstreamwriter (system. Out, "gb2312 ");
For example, the following example of Java encoding meets the requirements:
// Read. Java
Import java. Io .*;
Public class read {
Public static void main (string [] ARGs) throws ioexception {
String STR = "Chinese test, this is an internal hard-coded string" + "test English character ";
String strin = "";
Bufferedreader stdin = new bufferedreader (New inputstreamreader (system. In, "gb2312"); // sets the input interface to be encoded in Chinese.
Bufferedwriter stdout = new bufferedwriter (New outputstreamwriter (system. Out, "gb2312"); // sets the output interface to be encoded in Chinese.
Stdout. Write ("Enter :");
Stdout. Flush ();
Strin = stdin. Readline ();
Stdout. Write ("this is from the user input string:" + strin );
Stdout. Write (STR );
Stdout. Flush ();
}}
At the same time, we use the following methods to compile the program:
Javac-encoding gb2312 read. Java
The running result 5 is as follows:

Figure 5
2. Support classes for EJB classes and those that cannot be directly run (such as JavaBean classes)
Because they are called by other classes and do not directly interact with users, we recommend that the internal program use the character stream to process the Chinese character strings in the Program (as in the previous section). At the same time, when compiling a class, use the-encoding gb2312 parameter to indicate that the source file is encoded in Chinese format.
3. For Servlet
For servlet, we recommend that you use the following methods:
When compiling the source program of the servlet class, use-encoding to specify the Encoding As GBK or gb2312, and use the setcontenttype ("text/html; charset = GBK "); or gb2312 to set the output encoding format. Similarly, when receiving user input, we use request. setcharacterencoding ("gb2312"); in this way, no matter which operating system our servlet class is transplanted to, only the browser of the client supports Chinese display. The following is a correct example:
// Helloworld. Java
Package hello;
Import java. Io .*;
Import javax. servlet .*;
Import javax. servlet. http .*;
Public class helloworld extends httpservlet
{
Public void Init () throws servletexception {}
Public void doget (httpservletrequest request, httpservletresponse response) throws ioexception, servletexception
{
Request. setcharacterencoding ("gb2312"); // you can specify the input encoding format.
Response. setcontenttype ("text/html; charset = gb2312"); // you can specify the output encoding format.
Printwriter out = response. getwriter (); // printwriter output is recommended.
Out. println ("<HR> ");
Out. println ("Hello world! This is created by Servlet! Test Chinese! ");
Out. println ("<HR> ");
}
Public void dopost (httpservletrequest request, httpservletresponse response) throws ioexception, servletexception
{
Request. setcharacterencoding ("gb2312"); // you can specify the input encoding format.
Response. setcontenttype ("text/html; charset = gb2312"); // you can specify the output encoding format.
String name = request. getparameter ("name ");
String id = request. getparameter ("ID ");
If (name = NULL) name = "";
If (ID = NULL) id = "";
Printwriter out = response. getwriter (); // printwriter output is recommended.
Out. println ("<HR> ");
Out. println ("your input Chinese string is:" + name );
Out. println ("<HR> the id you entered is:" + id );
Out. println ("<HR> ");
}
Public void destroy (){}
}
Use javac-encoding gb2312 helloworld. Java to compile this program.
The Program for testing this servlet is as follows:
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% Request. setcharacterencoding ("gb2312"); %>
<HTML> <Script language = "JavaScript">
Function submit (){
// Pass the Chinese string value to servlet through URL
Document. Base. Action = "./helloworld? Name = Chinese ";
Document. Base. method = "Post ";
Document. Base. Submit ();
}
</SCRIPT>
</Head>
<Body bgcolor = "# ffffff" text = "#000000" topmargin = "5">
<Form name = "base" method = "Post" target = "_ Self">
<Input name = "ID" type = "text" value = "" size = "30">
<A href = "javascript: Submit ()"> send to servlet </a>
</Form> </body> The running result 6 is as follows:

Figure 6
4. Between Java programs and databases
To avoid gibberish during data transmission between Java programs and databases, we recommend that you use the following optimal methods:
1. the Java program is processed in the method we specify.
2. Change the default supported encoding format of the database to GBK or gb2312.
For example, in MySQL, we can add the following statement in the configuration file my. ini:
Add the following in the [mysqld] area:
Default-character-set = GBK
And added:
[Client]
Default-character-set = GBK
In SQL Server 2 K, we can set the default language of the database to simplified Chinese.
5. JSP code
Since JSP is dynamically compiled by the Web container at runtime, if the encoding format of the JSP source file is not specified, the JSP compiler will obtain the file of the server operating system. the value of encoding is used to compile JSP files. It is the most prone to problems during transplantation. For example, if a JSP file that is lucky enough in the Chinese Win2k language cannot be obtained in English Linux, although the client is the same, it is because the encoding of the operating system obtained by the container when compiling the JSP file is different (in the Chinese wink file. encoding and file in English Linux. encoding is different, and the file in English Linux. encoding does not support Chinese characters, so the compiled JSP class will be faulty ). Most of the issues discussed on the network are these problems, mostly because they cannot be correctly displayed when the JSP file is transplanted to the platform. For these problems, we understand the principle of program encoding conversion in Java, it is much easier to solve. The recommended solution is as follows:
1. We need to ensure that the JSP is output in Chinese encoding to the client. In any case, we first Add the following line to our JSP Source Code:
<% @ Page contenttype = "text/html; charset = gb2312" %>
2. In order for JSP to correctly obtain the input parameters, we add the following sentence to the JSP Source File Header:
<% Request. setcharacterencoding ("gb2312"); %>
3. In order for the JSP compiler to correctly decode our JSP file containing Chinese characters, We need to specify the encoding format of our JSP source file in the JSP source file. Specifically, add the following sentence to the JSP Source File Header:
<% @ Page pageencoding = "gb2312" %> or <% @ page pageencoding = "GBK" %>
This is a newly added instruction in JSP specification 2.0.
We recommend that you use this method to solve the problem of Chinese Characters in JSP files. The following code is a test program for JSP files in the correct way:
// Testchinese. jsp
<% @ Page pageencoding = "gb2312" %>
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% Request. setcharacterencoding ("gb2312"); %>
<%
String action = request. getparameter ("action ");
String name = "";
String STR = "";
If (action! = NULL & Action. Equals ("sent "))
{
Name = request. getparameter ("name ");
STR = request. getparameter ("str ");
}
%>
<HTML>
<Head>
<Title> </title>
<Script language = "JavaScript">
Function submit ()
{
Document. Base. Action = "? Action = sent & STR = input Chinese ";
Document. Base. method = "Post ";
Document. Base. Submit ();
}
</SCRIPT>
</Head>
<Body bgcolor = "# ffffff" text = "#000000" topmargin = "5">
<Form name = "base" method = "Post" target = "_ Self">
<Input type = "text" name = "name" value = "" size = "30">
<A href = "javascript: Submit ()"> submit </a>
</Form>
<%
If (action! = NULL & Action. Equals ("sent "))
{
Out. println ("<br> the character you entered is:" + name );
Out. println ("<br> the character you pass through the URL is:" + Str );
}
%>
</Body>
</Html>
7 is the result of the program running:

Figure 7
5. Summary
In the above detailed analysis, we clearly show the detailed conversion process of Java in the Process of processing the source program, which provides a basis for us to correctly solve the Chinese problems in Java programming. At the same time, we give a solution that is the best to solve the Java Chinese problem.
 

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.