Java/jsp Chinese garbled problem Solutions

Source: Internet
Author: User

Since its access to Java and JSP, it has been constantly dealing with Java's Chinese garbled characters, and now it has finally been completely solved. We will share our solutions with you. </P> <p> 1. Java Chinese problems </P> <p> JAVA kernel and class files are Unicode-based, this makes Java <SPAN class = 'wp _ keywordlink '> Program </span> good cross-platform, but it also brings some trouble about Chinese garbled characters. There are two main reasons: the garbled problem generated during compilation of Java and JSP files and the garbled problem caused by interaction between Java programs in other media. </P> <p> first, Java (including JSP) source files may contain Chinese characters, while Java and JSP source files are saved Based on byte streams, if the encoding method used when Java and JSP are compiled into a class file is inconsistent with that of the source file, garbled characters may occur. Based on this Garbled text, it is recommended that you do not write Chinese characters in the Java file as much as possible (the comments do not participate in compilation, and it is okay to write Chinese characters). If necessary, manually include the-ecoding GBK or-ecoding gb2312 parameter. for JSP, you can add or basically solve this type of garbled code problem in the file header. </P> <p> This article focuses on the second type of garbled code, that is, garbled code generated when Java programs interact with other storage media. Many storage media, such as databases, files, and streams, are stored Based on byte streams. When Java programs interact with these media, character (char) and byte (byte) occur) the conversion between them is as follows: </P> <p> submit data from the form to the Java program byte-> char </P> <p> display char from the Java program to the page?> Byte </P> <p> from database to Java program byte?> Char </P> <p> from Java program to database Char?> Byte </P> <p> from file to Java program byte-> char </P> <p> from Java program to file char-> byte </P> <p> from the stream to the Java program byte-> char </P> <p> from the Java program to the stream char-> byte </P> <p> If the encoding used during the above conversion process the encoding method is inconsistent with the original Byte encoding, garbled characters may occur. </P> <p> 2. Solution </P> <p> the conversion process of characters and bytes when the Java program interacts with other media, if garbled characters are generated during these conversions. The key to solving these garbled characters is to ensure that the encoding method used during conversion is consistent with the original Byte encoding method. The following describes the garbled characters generated by Java or JSP (see section 1 ). </P> <p> 1. garbled characters between JSP and page parameters </P> <p> JSP generally uses the default encoding method when obtaining page parameters, if the encoding type of the page parameter is inconsistent with the default encoding type, garbled characters may occur. The basic method to solve this type of Garbled text problem is to forcibly specify the request parameter encoding method before obtaining parameters on the page: request. setcharacterencoding ("GBK") or request. setcharacterencoding ("gb2312 "). </P> <p> If garbled characters appear when JSP outputs variables to the page, you can set response. setcontenttype ("text/html; charset = GBK") or response. setcontenttype ("text/html; charset = gb2312. </P> <p> if you do not want to write these two sentences in each file, you can use the transfer handler in the servlet specification to specify the encoding. the typical configuration and main <SPAN class = 'wp _ keywordlink '> Code </span> in XML are as follows: </P> <p> Web. XML: </P> <p> characterencodingfilter </P> <p> net. vschool. web. characterencodingfilter </P> <p> encodinggbk </P> <p> characterencodingfilter </P> <p>/* </P> <p> characterencodingfilter. java: </P> <p> public class characterencodingfilter implements filter </P> <p >{</P> <p> protected strin G encoding = NULL; </P> <p> Public void Init (filterconfig) throws servletexception </P> <p >{</P> <p> This. encoding = filterconfig. getinitparameter ("encoding"); </P> <p >}</P> <p> Public void dofilter (servletrequest request, servletresponse response, filterchain chain) throws ioexception, servletexception </P> <p >{</P> <p> request. setcharacterencoding (encoding); </P> <p> response. setcontenttype (" Text/html; charset = "+ encoding); </P> <p> chain. dofilter (request, response ); </P> <p >}</P> <p> 2. garbled characters between Java and databases </P> <p> most databases supports Unicode encoding, therefore, it is wise to solve the garbled code problem between Java and the database by directly using Unicode encoding to interact with the database. Many database drivers automatically support Unicode, such as Microsoft's sqlserver driver. Most other database drivers can be specified in the driver URL parameters, for example, MySQL driver of MM: JDBC: mysql: // localhost/webcldb? Useunicode = true & characterencoding = GBK. </P> <p> 3. garbled characters between Java and files/streams </P> <p> the most common classes for reading and writing files in Java are fileinputstream, fileoutputstream, and filereader/filewriter. Fileinputstream and fileoutputstream are based on byte streams and are often used to read and write binary files. We recommend that you use character-based filereader and filewriter to read and write character files, eliminating the need for conversion between bytes and characters. However, the constructors of these two classes use the system encoding method by default. If the file content is inconsistent with the system encoding method, garbled characters may occur. In this case, we recommend that you use the parent class of filereader and filewriter: inputstreamreader/outputstreamwriter. They are character-based, but you can specify the encoding type in the constructor: inputstreamreader (inputstream in, charset CS) and outputstreamwriter (outputstream out, charset CS ). </P> <p> 4. Others </P> <p> the method mentioned above should be able to solve most of the garbled issues. If garbled code occurs elsewhere, you may need to manually modify the code. The key to solving Java garbled characters is that during the conversion of bytes and characters, you must know the encoding method of the original or converted bytes, the encoding used for conversion must be consistent with the encoding method. Previously, we used the Resin server and the smartupload component to upload files. There was no garbled problem in obtaining the Chinese parameters passed while uploading files. When resin is set to a service in Linux, garbled characters are obtained for Chinese parameters while uploading files. This problem has plagued us for a long time. Later we analyzed the source file of the smartupload component, because the file upload adopts the byte stream mode, and the parameter names and values contained in the parameter are also transmitted in the byte stream mode. The smartupload component reads the byte stream and then parses the parameter name and value from the byte stream. The problem occurs when smartupload uses the default encoding when converting byte streams into strings, when resin is set as a service, the default encoding of the system may change, so garbled characters may occur. Later, we changed the source file of smartupload and added the charset and setcharset (string) attributes to extract the parameter statement from the upload () method: </P> <p> string value = new string (m_binarray, m_startdata, (m_enddata-m_startdata) + 1 ); </P> <p> changed to </P> <p> string value = new string (m_binarray, m_startdata, (m_enddata-m_startdata) + 1, charset ); </P> <p> the garbled problem is finally solved. <Br/>  

Related Article

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.