How to solve Chinese garbled characters in Java/JSP

Source: Internet
Author: User

I. Why Java Chinese problems

The Java kernel and class files are unicode-based, which makes Java programs have good cross-platform performance, 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.

First, Java includes JSP.) The source file may contain Chinese characters, while the source files of Java and JSP are stored Based on byte streams. If Java and JSP are compiled into a class file, if the encoding method used is inconsistent with the source file encoding, garbled characters may occur. Based on this Garbled text, it is recommended that you do not write Chinese comments in Java files and do not compile them.) If you must write Chinese, 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.

This article focuses on the second type of Garbled text, that is, the Garbled text 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:

Submit data from the form on the page to the java program byte-> char

Display char from java program to page?> Byte

From database to java program byte?> Char

From java program to database char?> Byte

From file to java program byte-> char

From java program to file char-> byte

From the stream to the java program byte-> char

From java program to stream char-> byte

If the encoding method used in the above conversion process is inconsistent with the original Byte encoding, garbled characters may occur.

2. Solutions to Chinese garbled characters in Java/JSP

The conversion process of characters and bytes when the Java program interacts with other media is mentioned above. If the conversion process is prone to garbled characters. 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. For details about the garbled characters produced by Java or JSP, see section 1 ).

1. garbled characters between JSP and page Parameters

JSP generally uses the default encoding method when obtaining page parameters. If the encoding type of the page parameters is different from 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 ").

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.

If you do not want to write these two sentences in each file, you can use the handler in the Servlet specification to specify the encoding. The typical configuration and main code of the filter in web. xml are as follows:

 
 
  1. web.xml:  
  2. CharacterEncodingFilter  
  3. net.vschool.web.CharacterEncodingFilter  
  4. encodingGBK  
  5. CharacterEncodingFilter  
  6. /*  
  7. CharacterEncodingFilter.java:  
  8. public class CharacterEncodingFilter implements Filter  
  9. {  
  10. protected String encoding = null;  
  11. public void init(FilterConfig filterConfig) throws ServletException  
  12. {  
  13. this.encoding = filterConfig.getInitParameter("encoding");  
  14. }  
  15. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException  
  16. {  
  17. request.setCharacterEncoding(encoding);  
  18. response.setContentType("text/html;charset="+encoding);  
  19. chain.doFilter(request, response);  
  20. }  

2. garbled characters between Java and databases

Most databases support unicode encoding, so it is wise to solve the garbled problem between Java and databases 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.

3. garbled characters between Java and files/streams

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 ).

4. Others

The method mentioned above should be able to solve most of the garbled issues. If there are garbled characters 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:

 
 
  1. String value = new String(m_binArray, m_startData, (m_endData - m_startData) + 1 ); 

Changed

 
 
  1. String value = new String(m_binArray, m_startData, (m_endData - m_startData) + 1, charset ); 

Finally, the Java/JSP Chinese Garbled text problem is solved.

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.