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 ("
Test this Servlet program as follows: <% @ page contenttype = "text / html; charset = GB2312"%> <% Request.setCharacterencoding ("GB2312");%>