Java Chinese Solution Encyclopedia (next)

Source: Internet
Author: User
Solve | Chinese 4. Classification of Chinese problems and the optimal solution

After understanding the principle of Java processing file, we can put forward a set of optimal solution to the problem of Chinese characters.
Our goal is: we edited in the Chinese system contains Chinese strings or Chinese processing Java source program can be compiled to move the value of any other operating system to run correctly, or to get the other operating system compiled to run correctly, can correctly transfer Chinese and English parameters, Able to communicate with database correctly and in English string.
Our specific ideas are: In the Java program transcoding entry and Exit and Java program with the user has input and output conversion of the local limit coding method to make it correct.

specific solutions are as follows:

1. For classes running directly on the console
In this case, we recommend that when the program is written, if you need to receive the user from the client may contain Chinese input or contain Chinese output, the program should use the character stream to process input and output, in particular, apply the following character-oriented node flow type:
To file: Filereader,filewrieter
Its byte type node stream type is: Fileinputstream,fileoutputstream
Pairs of Memory (array): Chararrayreader,chararraywriter
Its byte type node stream type is: Bytearrayinputstream,bytearrayoutputstream
For memory (string): Stringreader,stringwriter
To pipe: pipedreader,pipedwriter
Its byte type node stream type is: Pipedinputstream,pipedoutputstream
At the same time, input and output should be processed using the following character-oriented processing streams:
Bufferedwriter,bufferedreader
The processing flow of its byte type is: Bufferedinputestream,bufferedoutputstream
Inputstreamreader,outputstreamwriter
The processing flow of its byte type is: Datainputstream,dataoutputstream
Where InputStreamReader and inputstreamwriter are used to convert a byte stream to a character string according to the specified set of characters, such as:
InputStreamReader in = new InputStreamReader (system.in, "GB2312");
OutputStreamWriter out = new OutputStreamWriter (System.out, "GB2312");
For example, the following sample Java encoding is required:

   //read.java
    import java.io.*;
    public class Read {
    public static void Main (string[] args) throws IOException {    string str = "\ n Chinese test, this is the internal hard-coded string" + "\ntest 中文版 character";
    String strin= "";
    BufferedReader stdin = new BufferedReader (new InputStreamReader (system.in, "gb2312")); Set input interface by Chinese encoding
    bufferedwriter stdout = new BufferedWriter (new OutputStreamWriter (System.out, " gb2312 ")); Set the output interface to be encoded in Chinese
    stdout.write ("Please enter:");
    Stdout.flush ();
    Strin = Stdin.readline ();
    Stdout.write ("This is a string entered from the User:" +strin);
    stdout.write (str);
    Stdout.flush ();
   }}
    at the same time, when compiling a program, we do it in the following ways:
    javac-encoding gb2312 Read.java
     The results of its operation are shown in Figure 5:

Figure 5
2, for the EJB class and can not run directly support classes (such as JavaBean Class)

Because the classes themselves are called by other classes, do not directly interact with the user, so for this class, our recommended approach is that the internal program should use the character stream to handle the Chinese string inside the program (as in the previous section), while compiling the class with the-encoding The gb2312 parameter indicates that the source file is encoded in Chinese format.


3, for the servlet class

For the servlet, we recommend the following methods:

When compiling the source program of the Servlet class, specify the encoding as GBK or GB2312 with-encoding, and the setContentType ("TEXT/HTML;CHARSET=GBK") of the response object in the encoding part of the output to the user; or gb2312 to set the output encoding format, we use Request.setcharacterencoding ("GB2312") when receiving user input, so no matter what OS our servlet class is ported to, Only the client's browser supports Chinese display, it can be displayed correctly. 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"); Set input encoding format
Response.setcontenttype ("text/html;charset=gb2312"); Set Output encoding format
PrintWriter out = Response.getwriter (); Recommended use of PrintWriter output
Out.println ("Out.println ("Hello world! This is created test Chinese by servlet!! ");
Out.println ("}
public void DoPost (HttpServletRequest request, httpservletresponse response) throws IOException, Servletexception
{
Request.setcharacterencoding ("GB2312"); Set input encoding format
Response.setcontenttype ("text/html;charset=gb2312"); Set 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 (); Recommended use of PrintWriter output
Out.println ("Out.println ("The text string you pass in is:" + name);
OUT.PRINTLN ("Out.println ("}
public void Destroy () {}
}
Please compile this program with javac-encoding gb2312 Helloworld.java.
The program that tests this servlet looks like this:
<% @page contenttype= "text/html; charset=gb2312 "%>
<%request.setcharacterencoding ("GB2312");%>
<script language= "JavaScript" >
function Submit () {
Passing Chinese string values to the servlet by URL
Document.base.action = "./helloworld?name= Chinese";
Document.base.method = "POST";
Document.base.submit ();
}
</Script>

<body bgcolor= "#FFFFFF" text= "#000000" topmargin= "5" >
<form Name= "base" method = "POST" target= "_self" >
<input name= "id" type= "text" value= "size=" >
<a href = "javascript:submit ()" > Pass to Servlet</a>
</form></body>The results of the operation are shown in Figure 6:

Figure 6
4. Between Java program and database

To avoid garbled data passing between Java programs and databases, we recommend the following best methods to handle:
1. The processing method of Java program is handled according to the method we specify.
2. Change the encoding format supported by the database default to GBK or GB2312.

For example: In MySQL, we can add the following statement to the configuration file My.ini:
Increase in [mysqld] area:
Default-character-set=gbk
and Increase:
[Client]
Default-character-set=gbk
In SQL server2k, we can set the database default language to Simplified Chinese to achieve the goal.

5, for JSP code

Because the JSP is dynamically compiled by the Web container at run time, if we do not specify the encoding format of the JSP source file, The JSP compiler will get the file.encoding value of the server operating system to compile the JSP file, it is most likely to have problems when porting, such as in Chinese win2k can be very good to run the JSP file to get English Linux is not, although the client is the same, that is because the container in the compilation of JSP files to get the exercise As a result of the different coding of the system (in the Chinese wink file.encoding and in the English Linux file.encoding is different, and the English Linux file.encoding to Chinese does not support, so the compiled JSP class will have problems). Most of the discussion on the network is such a problem, mostly because the JSP file porting platform does not correctly display the problem, for this kind of problem, we understand the Java program code conversion principle, to solve it is much easier. The solutions we propose are as follows:

1, we want to ensure that the JSP output to the client is encoded in Chinese output, that is, in any case, we first in our JSP source generation to add the following line:

<% @page contenttype= "text/html; charset=gb2312 "%>
2, in order to allow the JSP to get the parameters passed in correctly, we add the following sentence to the JSP source file header:
<%request.setcharacterencoding ("GB2312");%>
3, in order to allow the JSP compiler to correctly decode our JSP files containing Chinese characters, we need to specify our JSP source files in the JSP source file encoding format, specifically, we are in the JSP source file to add the following sentence can be:
<% @page pageencoding= "GB2312"%> or <% @page pageencoding= "GBK"%>
This is the JSP Specification 2.0 the newly added instruction.
We recommend using this method to solve the Chinese problem in the JSP file, the following code is a correct approach to the JSP file test program:

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");
}
%>
<title></title>
<script language= "JavaScript" >
function Submit ()
{
Document.base.action = "? Action=sent&str= incoming Chinese ";
Document.base.method = "POST";
Document.base.submit ();
}
</Script>
<body bgcolor= "#FFFFFF" text= "#000000" topmargin= "5" >
<form Name= "base" method = "POST" target= "_self" >
<input type= "text" name= "name" value= "size=" >
<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 characters you pass through the URL are:" +str);
}
%>
</body>
Figure 7 is a schematic of the results of this program's operation:

Figure 7

5, summary

In the detailed analysis above, we clearly give Java in the process of processing the source program in the detailed conversion process, for us to correctly solve the Java programming in the Chinese problem provides a foundation. At the same time, we give the best solution to the problem of Java Chinese.

6. Reference materials
1, De Minghui. Analysis and solution of Chinese character problem in Java programming technology.
Http://www-900.ibm.com/developerWorks/cn/java/java_chinese/index.shtml
2, Zhou. Several principles of analysis on Java Chinese problems
Http://www-900.ibm.com/developerWorks/cn/java/l-javachinese/index.shtml



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.