The ultimate solution for Java Chinese garbled characters (to eliminate Chinese garbled demons by cutting the root)

Source: Internet
Author: User

Some time ago, the company developed a back-end management system for feinar travel network. My colleagues and I were the first to truly participate in such large-scale project development. During the development process, we have also encountered Chinese garbled characters for several times, and we are very confused and worried. After going online to check whether the so-called solutions are ineffective, after some twists and turns, we finally completely solved this problem. Based on comprehensive information and multiple considerations, we finally come up with our own solution:

First, make sure that your database supports Chinese. Set the encoding method of the database to support Chinese characters, such as gb2132 and UTF-8. After testing, the database can write Chinese characters and display them normally. This ensures that the database supports Chinese characters and can normally input and save Chinese characters. If the database itself does not support Chinese characters, no matter how you write the code, it will certainly be useless, and there will be Chinese garbled characters.

Second, avoid Chinese garbled characters from the input source. Only when Chinese garbled characters are eliminated from the data input source can the correct characters be entered into the database. In addition, the first step is guaranteed, therefore, the correctness of the data read from the database is ensured, so that the data read by the program from the database will not contain Chinese garbled characters. Solution:

Solution 1: eliminate the possibility of garbled characters from the page Input
1. Add the following content to the JSP input page:
<% @ Pagecontenttype = "text/html; charset =Database Character Set"%>
2. Add the first line of the HTML tag
<Meta http-equiv = "Content-Type" content = "text/html; charset =Database Character Set"/>

Solution 2: Use javax. servlet. Filter
1. Write web. xml configuration. In
This ensures that the data sent from the page is encoded in the same way as the database code. The data read from the database is the correct character you want, so you can read it with confidence.

<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. Compile 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,
Servletresponse, 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) throws servletexception ...{
This. Config = filterconfig;
System. Out. println ("*** init encodingfilter ");

}

}

 

Finally, avoid garbled characters in the output. Only when the output is well controlled will the previous work not be in vain, and the previous hard work will be recognized and rewarded. This step is very simple and can even be avoided, because we have basically done this step in the previous two steps. I mean, if the encoding is standard. However,The treasure of a thousand miles breaks the ant nest! Half a ninety-nine shopping spree!I hope that the preparations will not be futile. This is what we did not mean to see. Therefore, I suggest not miss the harvest season because of such a small step in governance. Solution:

Now, you only need to add
Request. setcharacterencoding (Database Character Set);
Response. setcharacterencoding (Database Character Set);
On the JSP data display page, add
<% @ Pagecontenttype = "text/html; charset =Database Character Set"%>
<Meta http-equiv = "Content-Type" content = "text/html; charset =Database Character Set"/>
Throughout the process, from the source to the source end, we thoroughly monitor every action of the data, and constantly correct the data appearance and flow, so as to completely eliminate the appearance of Chinese garbled characters.

Source: http://blog.csdn.net/morecans/archive/2007/07/15/1691860.aspx-column in morecans

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.