Thoroughly treat garbled diseases-J2EE. Web application Chinese problem Solutions

Source: Internet
Author: User
For a beginner in Java, a common problem may be Chinese. In fact, it is normal that we have a long history of thousands of years. It is profound and profound. Can it be tolerated by a small computer language? A joke. But the undeniable fact is that even a relatively experienced developer often suffers from gibberish on the screen.

Do you want to solve this problem thoroughly? Follow me ......

Garbled
The Java kernel is Unicode. However, Java always determines the initial encoding of a string based on the default encoding Character Set of the operating system, and the input and output of the Java System are all encoded by the default Operating System, the byte stream in database, file, and network transmission ...... The encoding used is different. So there will inevitably be annoying garbled characters.

Solution
1. gb2312, GBK, Unicode (utf8 )?
Compare the size of the character set to gb2312 <GBK <utf8. Obviously, if we use utf8 as the system encoding, there will be no error. And if you want to consider internationalization, utf8 seems to be your only choice.
2. Specify character set as UTF-8 when developing and compiling code
Both JBuilder and eclipse can be set in project properties.
3. Use a filter
Compile a filter

Package com. javer. Test. language;

Import javax. servlet. filter;
Import javax. servlet. filterchain;
Import javax. servlet. filterconfig;
Import javax. servlet. servletrequest;
Import javax. servlet. servletresponse;

/**
* <P> title: encodingfilter </P>
* @ Author javer (QQ: 84831612)
* @ Version 1.0
* @ Link http://blog.csdn.net/java008
*/
Public class encodingfilter
Implements Filter
{
Filterconfig config;

Public void Init (filterconfig parm1)
Throws javax. servlet. servletexception
{
This. Config = parm1;
}

Public void dofilter (servletrequest req, servletresponse res, filterchain chain)
Throws java. Io. ioexception, javax. servlet. servletexception
{
If (req. getcharacterencoding () = NULL |! Req. getcharacterencoding (). Equals ("UTF-8 "))
{
Req. setcharacterencoding ("UTF-8 ");
}
Chain. dofilter (req, Res );
}

Public void destroy ()
{
This. Config = NULL;
}
}

Configure the filter in the web. xml file

<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype web-app public "-// Sun Microsystems, Inc. // DTD web application 2.3 //" http://java.sun.com/dtd/web-app_2_3.dtd ">
<Web-app>
<Display-Name> javer's project </display-Name>
<Filter>
<Filter-Name> encodingfilter </filter-Name>
<Display-Name> encodingfilter </display-Name>
<Description> convert the encoding </description>
<Filter-class> com. javer. Test. Language. encodingfilter </filter-class>
</Filter>
<Filter-mapping>
<Filter-Name> encodingfilter </filter-Name>
<URL-pattern>/* </url-pattern>
</Filter-mapping>
<Welcome-file-List>
<Welcome-File> index. jsp </welcome-File>
</Welcome-file-List>
</Web-app>

4. Declare in JSP
In JSP header declaration <% @ page contenttype = "text/html; charset = UTF-8" %>
In jsp html code, declare <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
5. Database Management
General databases can set UTF-8 through management settings
You can also specify the encoding parameter through the JDBC link, for example, MySQL: JDBC: mysql: // localhost: 3306/test? Useunicode = true & characterencoding = UTF-8
6. Others
The UTF-8 can be set when encoding can be set for all other interactions with the outside world, such as reading files and operating XML. In short, remember one principle: utf8 encoding is used for "Translation" at the entrances and exits of all systems "!

  End! Good luck!

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.