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

zhaozj2021-02-16  50

In-depth analysis of Chinese issues and suggestions in Java programming: This article is the author original, the author contact address is: josserchai@yahoo.com. Since the Chinese issue in Java Programming is an older's all-talk, after reading many of the Java Chinese issues, combined with the author's programming practice, I found that many methods that have been discussed in the past cannot clearly explain the problem and solve problems, especially Chinese issues in cross-platform. So I gave this article, including the Chinese problem in the Class, Servelets, JSP, and EJB classes running in the console. I analyze and recommend a solution. I hope everyone will advise. Any reference this article please indicate the place! ! 4. The classification of Chinese issues and its proposed optimal solution After understanding the principles of JAVA processing files, we can put forward a solution to the best solution to Chinese characters. Our goal is to: we have a Chinese string or a Chinese string or a Chinese-processed Java source program that can be translated into any other operating system, or get it to compile other operating systems. Correctly run, correctly transmit Chinese and English parameters, accurately communicate with database strings. 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 solutions are as follows: 1. For this situation that is running directly on the CONSOLE, we recommend that when the program is written, if you need to receive the user's input or the output of the Chinese, the program should be used in the program. Character flows to process input and output, specifically, apply the following: FileReader, FileWrieTER whose byte type node stream type is: FileInputStream, FileOutputStream pairs memory (array): ChararrayReader, ChararrayWriter section node stream types: ByteArrayInputStream, ByteArrayOutputStream memory (string): StringReader, StringWriter pipeline: PipedReader, PipedWriter byte node whose stream type is: PipedInputStream, PipedOutputStream the same time, the following should be processed for character stream processing input and output: process flow BufferedWriter, BufferedReader byte which is: BufferedInputeStream, process flow BufferedOutputStream InputStreamReader, OutputStreamWriter byte which is: DataInputStream, DataOutputStream InputStreamReader and InputStreamWriter wherein the byte stream in accordance with a specified character set encoding conversion to the character stream, such as: InputStreamReader in = new InputStreamReader (System.in, "GB2312"); OutputStreamWriter out = new OutputStreamWriter (System.out, "GB2312"); for example: an example is as follows JAVA reached coding requirements: //Read.java import java.io. *; public class read {public static void main (string [] args) throws oews oExcepti ON {string str = "/ n Chinese test, this is internal hardcoded string" "/ ntest english character"; string strin = ""; bufferedreader stdin = new bufferedreader (new inputr (System.in, "GB2312") ); // Setting the input interface Press Chinese Encoding BufferedWriter Stdout = New BufferedWriter (NEW OTPUTSTREAMWRITER (SYSTEM.OUT, "GB2312")))))); // Setting the output interface Press Chinese Code 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, we use the following ways: javac -encoding GB2312 Read.java Operation results are shown in Figure 5: Figure 5 2, support for EJB classes and non-direct support (such as JavaBean classes), due to this class itself, is not directly interacting with the user, Therefore, for this type, our proposed handling method is the Chinese string should be used in the internal program to handle Chinese strings inside (as in the above section), while compiling class -Encoding GB2312 parameters Indicates the source file is the Chinese format encoding. 3, for the servlet class for servlet, we recommend using the following method: When compiling the source program of the Servlet class, use -encoding to specify the encoded to GBK or GB2312, and the encoding section when outputting to the user is setconTentType ("text" / html; charset = GBK "); or GB2312 to set the output encoding format, and when receiving user input, we use Request.setCharacterencoding (" GB2312 "); this is only customer, regardless of our servlet class transplantation The end 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 the input encoding format response.setContentType ( "text / html; charset = GB2312"); // Set the output encoding format PrintWriter out = response.getwriter (); // Recommended PrintWriter output Out.println ("


"); Out.println ("Hello World! This is created by servlet! Test Chinese!"); out.println ( "
");} public void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {request.setCharacterEncoding ( "GB2312"); // set the input encoding format response.setContentType ( "text / html ; "); // Setting 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 (); // It is recommended to use PrintWriter to output out.println ("
"); Out.println ("The Chinese string you are incorporated is:" Name); Out.println ("
you entered) ID is: " id); out.println ("
");} public void destroy ()}} Please use Javac -Encoding GB2312 HelloWorld.java to compile this program.

Test this Servlet program as follows: <% @ page contenttype = "text / html; charset = GB2312"%> <% Request.setCharacterencoding ("GB2312");%> </ Title> <script language = "javascript"> Function Submit () {// Pass the Chinese string value via URL to servlet Document.Base.Action = "./helloworld?name = Chinese"; document.base.method = "POST "; Document.Base.Submit ();} </ script> </ head> <body bgcolor =" # ffffff "text =" # 000000 "TopMargin =" 5 "> <form name =" method = "POST "Target =" _ self "> <input name =" id "type =" text "value =" "size =" 30 "> <a href = "javaScript:submit()"> pass to servlet </a> </ Form> </ body> </ html> The results are shown in Figure 6: Figure 6 4, the Java program and the database are garbled between the data transfer between the Java program and the database, we recommend the following best way To process: 1. Processing method for Java programs is handled according to the method we specified. 2. Change the encoding format supported by the database to GBK or GB2312. Such as: In MySQL, we can add the following statement implementation in the configuration file my.ini: DEFAULT-CHARACTER-SET = GBK and increases: [Client] default-character-set = GBK in SQL Server2k In, we can set the language of the database to Simplified Chinese to achieve the goal. 5. For the JSP code, since JSP is running, it is dynamically compiled by the web container. 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 prone to problems during transplantation. If the JSP file you can run in Chinese Win2k is not available in English Linux, although the client is the same, it is because the container is acquired when compiling JSP files. The coding of the operating system is different (file.Encoding in Chinese WINK and File.Encoding in English Linux is different, and English Linux's file.Encoding does not support Chinese, so there is a problem with the JSP classes compiled. ).</p> <p>Most of the number of discussions on the Internet is because the JSP file portal is not correctly displayed correctly. For such issues, we understand the principle of program encoding conversion in Java, and it is easy to solve it. Our solutions are as follows: 1. We must ensure that the JSP outputs the Chinese coding mode when the JSP is output, ie, we first add the following lines in our JSP source: <% @ Page ContentType = " Text / html; charset = GB2312 "%> 2, in order to get JSP to get the incoming parameters, we add the following sentence in the JSP source file header: <% request.setCharacterencoding (" GB2312 ");%> 3, in order to let The JSP compiler can correctly decode our JSP files containing Chinese characters. We need to specify the encoding format of our JSP source file in the JSP source file. Specifically, we will add the following sentence on the JSP source file. : <% @ Page PageEncoding = "GB2312"%> or <% @ Page PageEncoding = "GBK"%> This is a new JSP specification 2.0 added instructions.</p> <p>We recommend using this method to explain Chinese issues in the JSP file. The following code is a correct practicing 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 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 = Incoming 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> Your character is:" Name); Out.println ("<br> You pass the URL to the character:" STR);}%> </ body> </ html> Figure 7 is a schematic diagram of this program running: Figure 7 5, summarizing in the detailed analysis above, we Clearly give the Java's detailed conversion process in the process of handling the source program, providing the basis for our correct resolution Chinese issues in Java programming. At the same time, we give a solution to the best solution to Java Chinese issues.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-24525.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="24525" 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.038</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 = 'yHlWht85JMzQ_2Fz9A5r5jmDNhXXi9iQ0C_2FLESn8FgIhKpNuy1148smqnkBbAIgPVA2VJYtCIxusbUc7M8A1xPLQ_3D_3D'; 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>