The ultimate solution of Java Chinese garbled characters (extermination of Chinese garbled demon)

Source: Internet
Author: User

A period of time before the company to do Feinar Travel Network background Management system development, my colleagues and I are the first real participation in this large-scale project development, in the development process, we have encountered a number of Chinese garbled problems, is very confused and distressed, to the Internet to check, those so-called solutions are not working, after several setbacks, Finally solved the problem completely. Through the data and various aspects of the comprehensive consideration, finally came to their own solution:

First, make sure your database is supported in Chinese. The encoding of the database can be set to support Chinese, such as gb2132,utf-8 and so on, and after testing can actually write in Chinese and normal display. This ensures that the database does support Chinese, and that Chinese can be entered and saved normally. If the database itself does not support Chinese characters, then no matter how you write the code is useless, there will certainly be garbled.

Second, from the input source to eliminate the possibility of Chinese garbled. Only from the data input sources to eliminate the possibility of Chinese garbled, in order to ensure that the input to the database is the correct character, coupled with our first step of protection, then ensure that the data from the database is correct, thus ensuring that the program from the database read the data will not appear in Chinese garbled. Workaround:

Solution one: From the page input to eliminate the possibility of garbled
1, in the JSP input page to add:
<% @pagecontentType = "text/html;charset= database Character set "% >
2, in the HTML tag <meta http-equiv= "Content-type" content= "text/html; charset= Database Character set "/>

workaround two: Use Javax.servlet.Filter
1, write Web.xml configuration. In
This will ensure that the data sent by the page is encoded in accordance with the database code, so that the data read from the database is the correct character you want, you can safely read the < filter >
< filter-name > Encoding </filter-name >
< Filter-class > Com.morecans.test.EncodingFilter </filter-class >
< Init-param >
< Param-name > CharSet </param-name >
< Param-value > Utf-8 </param-value >
</Init-param >
</Filter >
< filter-mapping >
< filter-name > Encoding </filter-name >
< Url-pattern >/* </url-pattern >
</filter-mapping >


2, the preparation of Filterservlet:com.morecans.test.EncodingFilter
Package com.morecans.test;

Import java.io.IOException;

Import Javax.servlet.Filter;
Import Javax.servlet.FilterChain;
Import Javax.servlet.FilterConfig;
Import javax.servlet.ServletException;
Import Javax.servlet.ServletRequest;
Import Javax.servlet.ServletResponse;

public class Encodingfilter implements Filter ... {
Private Filterconfig config;

public void Destroy () ... {
SYSTEM.OUT.PRINTLN ("* * * Destroy Encodingfilter");

}

public void Dofilter (ServletRequest servletrequest,
Servletresponse Servletresponse, Filterchain filterchain)
Throws IOException, servletexception ... {
SYSTEM.OUT.PRINTLN ("* * * dofilter encodingfilter");
String charset = This.config.getInitParameter ("charset");
Servletrequest.setcharacterencoding (CharSet); Set
Servletresponse.setcharacterencoding (CharSet);
Filterchain.dofilter (ServletRequest, servletresponse);
}

public void init (Filterconfig filterconfig) throws Servletexception ... {
This.config = Filterconfig;
SYSTEM.OUT.PRINTLN ("* * * init encodingfilter");

}

}

Finally, in the output to avoid the appearance of garbled. Only the output control is good, then our work in the front is not in vain, before the hard will be recognized, can be harvested. This step, very simple, even can not, because we have two steps ahead of the basic work of this step has been done, I mean, if coding specifications. However, sink, Shan. Beholder hope that the preparation of the previous work will not be futile, this is what we do not want to see, so in governance I still suggest not because of such a small step missed the harvest season. Workaround:

Now you only need to add
request.setcharacterencoding ( database character set ) before reading the data;      
Response.setcharacterencoding ( database character set );  
While the JSP data display page is also the same as the output page
<%@ Pagecontenttype= "text/html;charset= database Character Set "% >                                  
<meta http-equiv= "Content-type" content= text/html; charset= Database character Set />
The whole process, from the source to the source, thoroughly monitor the data every move, the time to correct the appearance and flow of data, so as to completely eliminate the Chinese character garbled this demon's appearance.

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.