In-depth analysis of Chinese issues in Java programming and suggestion optimal solutions - Next

xiaoxiao2021-03-06  73

4. Classification of Chinese issues and its suggestion optimal solution

After understanding the principles of the above Java handling files, we can put forward a suggestible to solve the Chinese character problem.

Method.

Our goal is to: we have a Chinese string or a Chinese string or a Chinese-handed Java source program edited in the Chinese system.

After compiling, you can move correctly in any other operating system, or you can correctly run after compiling in other operating systems, you can correctly pass the Chinese and English parameters, accurately communicate with the database to communicate with the database.

Our specific ideas are: in the port and exports of the Java program transcoding and the Java program limit the encoding method to limit the encoding method to the user.

The specific solution is as follows:

1. A class that is running directly on the Console

In this case, we recommend that when the program is written, if you need to receive users from the user, it may contain Chinese.

Enter or contain the Chinese output, and the character flow should be used in the program to process input and output. Specifically, apply the following to the characteristic node stream type:

For files: FileReader, FileWrieter

Its byte node stream is: FileInputStream, FileOutputStream

For memory (arrays): ChararrayReader, ChararrayWriter

Its byte nodes are: byterrayinputstream, ByteaRrayoutputstream

For memory (string): StringReader, StringWriter

Tongue: PipedReader, PiPedWriter

Its byte nodes are: pipedinputstream, pipedoutputstream

At the same time, you should use the following to the characteristic processing flow to process the input and output:

BufferedWriter, BufferedReader

Its byte type processing flow is: bufferedInputStream, BufferedoutputStream

InputStreamReader, OutputStreamwriter

Its byte type processing flow is: DataInputStream, DataOutputStream

The InputStreamReader and InputStreamWriter are used to convert byte streams according to the specified character encoding set.

To character stream, such as:

InputStreamReader IN = New InputStreamReader (System.in, "GB2312");

OutputStreamWriter out = new outputStreamWriter (System.out, "GB2312");

For example: the following example Java coding has reached the requirements:

//Read.java

Import java.io. *;

Public class read {

Public static void main (string [] args) throws oException {

String str = "/ n Chinese test, this is internal hardcoded string" "/ ntest english character";

String strin = "";

BufferedReader stdin = new buffredreader (New InputStreamReader (System.in, "GB2312")); // Setting Input Interface Press Chinese Coding

BufferedWriter stdout = new buffsetwriter (New OutputStreamwriter (System.out, "G

B2312 ")); // Set the output interface Press Chinese encoding

STDOUT.WRITE ("Please enter:");

STDOUT.FLUSH ();

Strin = stdin.readline ();

Stdout.write ("This is the string input from the user:" Strin);

STDOUT.WRITE (STR);

STDOUT.FLUSH ();

}

At the same time, when compiling the program, we use the following way:

Javac -ENCODING GB2312 Read.java

Its operation results are shown in Figure 5:

Figure 5

2, support for EJB classes and not directly running (such as Javabean classes)

Because this type itself is called by other classes, it is not directly interacting with the user, so our construction is for this kind.

The processing method of the discussion is that the character stream should be used in the internal program to handle the Chinese string inside (as in the above section), while in the compiling class, using the -Encoding GB2312 parameter indication source file is the Chinese format encoded. can.

3, targeting the servlet class

For servlet, we recommend the following methods:

When compiling the source program of the Servlet class, specify the encoded as GBK or GB2312 with -Encoding, and output to the user

The encoding section uses the setContentType ("text / html; charset = GBK") of the Response object; or GB2312

Set the output encoding format, and when you receive user input, we use Request.SetCharacterencoding ("GB231

2 "); this way, whether our Servlet class is transplanted in what operating system, 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) THR

OWS IOException, ServletException

{

Request.setCharacterencoding ("GB2312"); // Setting Input Coding Format

Response.setContentType ("text / html; charset = GB2312"); // Set the output encoding format

PrintWriter Out = response.getwriter (); // Recommended using PrintWriter output

Out.println ("


");

Out.println ("Hello World! this is created by servlet! Test Chinese);

Out.println ("


");

Public void dopost (httpservletRequest Request, HttpservletResponse Response) TH

Rows oException, servletexception

{

Request.setCharacterencoding ("GB2312"); // Setting Input Coding Format

Response.setContentType ("text / html; charset = GB2312"); // Set 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 (); // Recommended using PrintWriter output

Out.println ("


");

Out.println ("The Chinese string you are incorporated is:" Name);

Out.println ("


The id you entered is:" ID);

Out.println ("


");

}

Public void destroy () {}

}

Please compile this program with Javac -Encoding GB2312 HelloWorld.java.

Testing this servlet is as follows:

<% @ Page ContentType = "text / html; charset = GB2312"%>

<% Request.SetCharacterencoding ("GB2312");%>

</ title></p> <p><Script language = "javascript"></p> <p>Function Submit () {</p> <p>// Pass the Chinese string value to servlet via URL</p> <p>Document.base.Action = "./helloworld?name= Chinese";</p> <p>Document.base.method = "post";</p> <p>Document.base.submit ();</p> <p>}</p> <p></ Script></p> <p></ hEAD></p> <p><body bgcolor = "# ffffff" text = "# 000000" TopMargin = "5"></p> <p><form name = "base" method = "post" target = "_ self"></p> <p><input name = "ID" type = "text" value = "" Size = "30"></p> <p><a href = "javaScript:submit()"> pass it to servlet </A></p> <p></ form> </ body> </ html></p> <p>Its operation results are shown in Figure 6:</p> <p>Figure 6</p> <p>4, between Java programs and databases</p> <p>In order to avoid garbled between the Java programs and database data transfer, we recommend using the following best ways</p> <p>Li:</p> <p>1. Processing method for Java programs is handled according to the method we specified.</p> <p>2. Change the encoding format supported by the database to GBK or GB2312.</p> <p>For example: In MySQL, we can add the following statement implementation in the configuration file my.ini:</p> <p>Increase in the [MySQLD] area:</p> <p>Default-character-set = GBK</p> <p>And increase:</p> <p>[client]</p> <p>Default-character-set = GBK</p> <p>In SQL Server2k, we can set the language of the database to Simplified Chinese to achieve the purpose.</p> <p>of.</p> <p>5, for JSP code</p> <p>Since the JSP is running during runtime, if we don't specify the code of the JSP source file</p> <p>Format, the JSP compiler will get the file.Encoding value of the server operating system to compile the JSP file, it is shifted</p> <p>It is most prone to problems during the plant, such as the JSP file that can be run in Chinese Win2k is not working in English Linux.</p> <p>The tube client is the same, that is because the container is different from the coding of the operating system acquired when compiling the JSP file (</p> <p>File.Encoding in Chinese Wink and File.Encoding in English, Different, and English Linux F</p> <p>Ile.Encoding does not support Chinese, so there is a problem with the JSP class compiled). Most of the discussion on the Internet is</p> <p>Such problems, mostly because the JSP file transplant platform does not correctly display the problem, we understand this problem</p> <p>The principle of program encoding conversion in Java is more likely to solve more. Our suggested solutions are as follows:</p> <p>1. We must ensure that the JSP is output to the client, which is output in Chinese coding, ie, we are first in</p> <p>Our JSP source is added to the following line:</p> <p><% @ Page ContentType = "text / html; charset = GB2312"%></p> <p>2, in order to get JSP to get the incoming parameters, we add the following sentence in the JSP source file:</p> <p><% Request.SetCharacterencoding ("GB2312");%></p> <p>3. In order to let the JSP compiler correctly decode our JSP file containing Chinese characters, we need to be in JSP sources</p> <p>Specify the encoding format of our JSP source file, specifically, we will add the following sentence on the JSP source file header:</p> <p><% @ Page PageEncoding = "GB2312"%> or <% @ Page PageEncoding = "GBK"%></p> <p>This is a newly added instruction for JSP specification 2.0.</p> <p>We recommend using this method to explain Chinese issues in the JSP file, the following code is a correct practical JSP file</p> <p>Test procedure:</p> <p>//testChinese.jsp</p> <p><% @ Page PageEncoding = "GB2312"%></p> <p><% @ Page ContentType = "text / html; charset = GB2312"%></p> <p><% Request.SetCharacterencoding ("GB2312");%></p> <p><%</p> <p>String action = request.getParameter ("action");</p> <p>String name = ""</p> <p>String str = ""; if (action! = Null && action.equals ("SENT")))</p> <p>{</p> <p>Name = Request.getParameter ("name");</p> <p>Str = Request.getParameter ("Str");</p> <p>}</p> <p>%></p> <p><html></p> <p><HEAD></p> <p><title> </ title></p> <p><Script language = "javascript"></p> <p>Function Submit ()</p> <p>{</p> <p>Document.base.Action = "? action = Sent & Str = incoming Chinese";</p> <p>Document.base.method = "post";</p> <p>Document.base.submit ();</p> <p>}</p> <p></ Script></p> <p></ hEAD></p> <p><body bgcolor = "# ffffff" text = "# 000000" TopMargin = "5"></p> <p><form name = "base" method = "post" target = "_ self"></p> <p><input type = "text" name = "value" value = "" size = "30"></p> <p><a href = "javascript:submit()"> submit </A></p> <p></ form></p> <p><%</p> <p>IF (action! = null && action.equals ("SENT"))</p> <p>{</p> <p>Out.println ("<br> The characters you entered are:" Name);</p> <p>Out.println ("<br> You are incorporated by URL:" STR);</p> <p>}</p> <p>%></p> <p></ body></p> <p></ html></p> <p>Figure 7 is a schematic diagram of the result of this program running:</p> <p>Figure 7</p> <p>5, summary</p> <p>In the detailed analysis above, we clearly give the Detailed conversion process of Java during the process of processing the source program.</p> <p>We correctly solve the Chinese problem in Java programming. At the same time, we give a solution to the best solution to Java Chinese issues.</p> <p>6, reference materials</p> <p>1. Analysis of Chinese character problems in Java programming technology.</p> <p>http://www-900.ibm.com/developerWorks/cn/java/java_chinese/index.shtml</p> <p>2, Zhou Jun Tao. Several analysis principles on java Chinese issues</p> <p>http://www-900.ibm.com/developerWorks/cn/java/l-javachinese/index.shtml</p> <p>7. Author's introduction</p> <p>Author: abnerchai, senior programmer, author Contact: josserchai@yahoo.com</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-109098.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="109098" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.049</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'hHMFPq37ZhOsOArfsMXW_2FeHyFZtwRqwvKWmhC_2BgQ3U_2BhVmza9khdpdlK9F6S9jwjnssRS86Wnniuk6z1'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>