Java JSP Chinese garbled Problem Solution

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.

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 (including JSP) source files may contain Chinese characters, while Java and JSP source files are stored Based on byte streams. If Java and JSP are compiled into class files, 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 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.

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.

Ii. Solution

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. The following describes the garbled characters generated 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:

Web. xml:

Characterencodingfilter

Net. vschool. Web. characterencodingfilter

Encodinggbk

Characterencodingfilter

/*

Characterencodingfilter. Java:

Public class characterencodingfilter implements Filter

{

Protected string encoding = NULL;

Public void Init (filterconfig) throws servletexception

{

This. Encoding = filterconfig. getinitparameter ("encoding ");

}

Public void dofilter (servletrequest request, servletresponse response, filterchain chain) throws ioexception, servletexception

{

Request. setcharacterencoding (encoding );

Response. setcontenttype ("text/html; charset =" + encoding );

Chain. dofilter (request, response );

}

}

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:

String value = new string (m_binarray, m_startdata, (m_enddata-m_startdata) + 1 );

Changed

String value = new string (m_binarray, m_startdata, (m_enddata-m_startdata) + 1, charset );

This garbled problem was finally solved.

From: http://www.enet.com.cn/article/2008/0229/A20080229170410.shtml

 

 

-------------- Add ---------------

Transferred from: http://yongtree.spaces.live.com/blog/cns! 9f387a11df33043d! 231. Entry

 

Chinese garbled characters are a headache for beginners of JSP web development. In the course of learning and working on projects, I am also annoyed by these problems. When there are many problems, I also have some experience. Now I will write them down and share them with my friends. 1. Chinese garbled characters output by JSP
To output Chinese Characters in JSP, you can directly output Chinese Characters in JSP, or assign Chinese values to the variables before outputting them, in this case, garbled characters are often caused by the absence of encoding methods for displaying Chinese characters on the JSP page. The solution is as follows:
1) Add the statement <% @ page contenttype = "text/html; charset = UTF-8" %> In the JSP page header (use httpservletresponse in servlet. setcontenttype ("text/html; charset = UTF-8"), it is best to add <meta http-equiv = "Content-Type" content = "text/html; charset = "UTF-8">
2) Actively convert the encoding method in the place where the Chinese characters are to be output. For example, to enter the word "Chinese" in the page, you can use the following method:
<%
String STR = "Chinese ";
Byte [] tmpbyte = Str. getbytes ("iso8859_1 ");
STR = new string (tmpbyte );
Out. println (STR );
%>
For the above two methods, it is clear that the first method is more common, you only need to add code once in a page; for the second method, transcoding is required for every place where Chinese characters are to be output. If there are many such places, this will be a heavy task.
2. Chinese garbled characters when retrieving data submitted by forms
Before any other processing is added, request. getparameter ("paramname") is used to obtain the data in the form submission. If the form data contains Chinese characters, the returned string is garbled. The reason for this problem is that the J2EE Implementation of Tomcat submits the form, that is, the parameters submitted in post mode are processed by the default ISO-8859-1.
There are two solutions to this problem:
1) do not modify other configurations, but convert the encoding after the Chinese data in the form is partitioned. The method is as follows: String STR = request. getparameter ("chstr"); string STR = new string (Str. getbytes ("ISO-8859-1"), "UTF-8"); but this method is only from a local to consider the problem, if such a situation is many, it is necessary to write many times, is bound to increase the workload.
2) allow requests to all pages to use a filter to set the processing character set to UTF-8 (you can also set it to another one as needed, such as gb2312 and GBK ). For more information, see the example in the webapps/servlet-exemples directory of Tomcat. You can also refer to the configuration of Web. xml and setcharacterencodingfilter.
3. Chinese Characters in URLs
For Chinese parameters such as http: // localhost: 8080/a. jsp? For GET requests such as STR = "Chinese", garbled characters are often returned when request. getparameter ("name") is used on the server side. It is useless to set the filter according to the above method. The request. setcharacterencoding ("UTF-8") method is still not used. The reason for this is that Tomcat uses a different processing method than the POST method to process the query-string request submitted in the get method.
To solve this problem, open the/CONF/server. xml file in the tomcat installation directory, find the connector block, and add urlencoding = "UTF-8"/>
4. garbled Characters During Database Access
The encoding methods of all tables in the database must be consistent with those used in JSP. This can reduce unnecessary encoding conversion problems. in addition, when using JDBC to connect to the MySQL database, writing the connection string as follows can avoid some Chinese problems:
JDBC: // MYSQL: // hostname: Port/dbname? User = username & Password = PWD & useunicode = true & character encoding = UTF-8
If you connect to the database as a data source, use the following in the configuration file:
<Parameter>
<Name> URL </Name>
<Value> JDBC: mysql: // hostname: Port/dbname? & Useunicode = true & characterencoding = UTF-8
</Value>
</Parameter>
However, if an existing database is used, the encoding method of the database is ISO-8859-1, while UTF-8 is used in Web applications, and the database has a lot of important information, therefore, you cannot change the encoding method of the database. At this time, when writing data to the database, be sure to add "useunicode = true & characterencoding = ISO-8859-1" to the JDBC connection string, so that the king database can smoothly write normal data. However, garbled characters may occur when reading data from the database. In this case, transcoding should be performed when the data is retrieved. You can write the transcoding function into a function. The specific implementation is as follows:
Public String charconvert (string SRC ){
String result = NULL;
If (SRC! = NULL ){
Try {
Result = new string (SRC. getbytes ("ISO-8859-1"), "UTF-8 ");
} Catch (exception e ){
Result = NULL;
}
}
Return result;
}
Therefore, charconvert (Rs. getstring ("colname") is called after the database reads data, so that the Chinese data in the database can be normally displayed.

 

 

 

-------------- Add ---------------


If garbled characters occur when parameters are transmitted on the page, you can use the encodeuri method to encapsulate the URL. For example

Encodeuri ("<% = request. getcontextpath () %>/dyfk/ajax/list. jsp? Id = "+ ID +" & isgxpcs = "+ isgxpcs +" & deptname = "+ deptname)

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.