In-depth java Chinese issues and optimal solutions - under (transfer)

xiaoxiao2021-03-06  38

4. Classification of Chinese issues and its suggestion optimal solution

After understanding the principles of Java processing files, we can put forward a solution to the best solution to solve 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 solution is as follows:

1. For this situation that is running directly on the CONSOLE, we recommend that when you write, if you need to receive the user from the user, you may have a Chinese input or an output of Chinese, the program should be used to process the input. And output, specifically, the following face-to-character node stream type: pair file: FileReader, FileWrieTer, FileInputStream, FileOutputStream pairs memory (array): ChararrayReader, CharaRrayWriter, CharaRrayWriter is: ByteArrayInputStream, ByteArrayOutputStream memory (string): StringReader, StringWriter pipeline: PipedReader, PipedWriter byte node whose stream type is: PipedInputStream, PipedOutputStream the same time, the input and output should be processed by the following processing flow for the character: BufferedWriter , the process flow is the byte which BufferedReader: BufferedInputeStream, process flow BufferedOutputStream InputStreamReader, OutputStreamWriter byte which is: DataInputStream, DataOutputStream wherein InputStreamWriter InputStreamReader and for converting the byte stream to the character stream according to the specified character code sets, Such as: InputStreamReader IN = New InputStreamReader (System.in, "GB2312"); OutputStreamWriter Out = New OutputStreamWriter (System.out, "GB2312"); for example: using the following example Java encoding requests:

//Read.java import java.io. *; public class read {public static void main (string [] args) throws ioException {string str = "/ n Chinese test, this is internal hard-coded string" "/ ntest english character "; String strin =" "; BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in," gb2312 ")); // input interface provided by the Chinese encoding BufferedWriter stdout = new BufferedWriter (new OutputStreamWriter (System.out, "GB2312")); // Set the output interface Press Chinese Code Stdout.Write ("Please enter:"); stdout.flush (); strin = stdin.readline (); stdout.write ("This is from User Enter String: " strin); stdout.write (STR); stdout.flush ();}} At the same time, we use the following ways when compiling: javac -encoding gb2312 read.java running results as shown in Figure 5 Schedule: Figure 52, support class for EJB and cannot run directly (such as JavaBean class)

Since this class itself is called, it is not directly interacting with the user. Therefore, for this type, our proposed handling method is a Chinese string that should be used in the internal program to handle the interior of the program (specifically as above As in one section), while compiling the class -Encoding GB2312 parameter indication source file is the Chinese format encoding.

3, targeting the servlet class

For servlet, we recommend the following methods:

When compiling the source program of the Servlet class, use -encoding to specify the encoded as GBK or GB2312, and the encoding portion when outputting to the user is set to setContentType ("text / html; charset = GBK"); or GB2312 to set the output In encoding format, when you receive user input, we use Request.setCharacterencoding ("GB2312"); this can be displayed correctly regardless of our Servlet class transplantation, only client browser supports Chinese display, you can display 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 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) throws IOException, ServletException {request.setCharacterEncoding (" GB2312 "); // set the input encoding 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 PrintWriter output Out.println ("
"); Out.println (" Your incoming Chinese string Yes: " name); Out.println ("
you entered) is: " id); out.println ("
");} public void destroy () {}} Please use Javac Encoding GB2312 HelloWorld.java to compile this procedure.

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 of its operation are shown in Figure 6:</p> <p>Figure 6 4, between the Java program and the database</p> <p>In order to avoid garbled between the Java program and the database, we recommend the following optimal methods to process: 1. Processing method for the Java program is handled by our specified method. 2. Change the encoding format supported by the database to GBK or GB2312.</p> <p>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.</p> <p>5, for JSP code</p> <p>Since the JSP is running, the web container is dynamically compiled, 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 transplant It is most prone to problems, such as the JSP file that can be run in Chinese Win2k is not available in English Linux, although the client is the same, that is because the container is encoded by the system when compiling the JSP file. Caused (file.Encoding in Chinese Wink and File.Encoding in English Linux), and English Linux's file.Encoding does not support Chinese, so the JSP class compiled will have a problem). 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 is output to the client to output, ie, we first add the following lines in our JSP source:</p> <p><% @ 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.setChacTerencoding ("GB2312") ;%> 3, in order to allow the JSP compiler to correctly decode our JSP files containing Chinese characters, we need to specify the code format of our JSP source file in the JSP source file, specifically, we are in the JSP source file header Join the following sentence: <% @ page PageEncoding = "GB2312"%> or <% @ Page PageEncoding = "GBK"%> This is a new JSP specification 2.0 added instructions. We recommend using this method to explain Chinese issues in the JSP file. The following code is a correct practical JSP file test program:</p> <p>//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 = Intrinsic Chinese; Document.Base.Method = "post"; document.base.submit ();} </ script> </ head> <body bgcolor = # ffffff "text =" # 000000 "TopMargin = "5"> <form name = "base" method = "post" target = "_ self" "name =" value "value =" "size =" 30 "> <a href =" JavaScript: Submit ()> Submit </a> </ form> <% IF (action! = Null && action.equals ("SENT")) {Out.println ("<br> Your character you entered is:" Name); Out.println ("<br> You are incorporated by URL:" Str);}%> </ bo DY> </ html> Figure 7 is a schematic diagram of the result of this program running: Figure 7</p> <p>5, summary</p> <p>In the above detailed analysis, we clearly give the Detailed conversion process of Java in the process of dealing with the source program, providing our foundation 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-68482.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="68482" 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.027</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 = 'jxM7476CsrLv2zqmjv7Y0AL35D05pHkRYXth6PHWfeFpuc7NgGoE4WeqbkDaQFMLE2JipbabSsa0s_2F5S'; 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>