Summary of garbled characters in Tomcat JSP web development

Source: Internet
Author: User

1. Static Page garbled
The file encoding is inconsistent with the encoding to be displayed in the browser.
1) Check the original file encoding. open the file in notepad and save it as a file;
2) Add a command to the current page to recommend that the browser use the specified encoding to display the file character content.
<Meta http-equiv = "Content-Type" content = "text/html; charset = GBK">
3) if the system is XP in English and the East Asian character set is not supported, garbled characters are displayed.

2. garbled JSP pages
1) The page command has a pageencoding = "GBK" which specifies the encoding of the current page to save. If it is written as a ISO8859-1, it cannot save Chinese characters;
2) page command contenttype = "text/html; charset = ISO8859-1" will also give the browser a priority to select a encoding like a static page.
If JSP code is garbled, it is usually displayed ?, And no matter what encoding you choose for the browser, it cannot be correctly displayed.

3. garbled form submission (tomcat-specific)
1). Post garbled characters

A. first, the encoding of the submitted form by the browser is determined based on the page where the form is located, rather than the code of the submitted JSP page. set the encoding of all pages to the same, for example, GBK.
B. The processing method is to set the encoding before obtaining the parameter:
Request. setcharacterencoding ("GBK ");
C. You can use a filter to solve the problem. Tomcat already has a ready-made one:
Apache-Tomcat-5.5.23 \ webapps \ JSP-examples \ WEB-INF \ Classes \ filters \ setcharacter
Encodingfilter. Java
Web. xml
<Filter>
<Filter-Name> set character encoding </filter-Name>
<Filter-class> filters. setcharacterencodingfilter </filter-class>
<Init-param>
<Param-Name> encoding </param-Name>
<Param-value> GBK </param-value>
</Init-param>
</Filter>
<Filter-mapping>
<Filter-Name> set character encoding </filter-Name>
<URL-pattern>/* </url-pattern>
</Filter-mapping>

 

2) garbled get
Setcharacterencoding () cannot solve a bug in. tomcat. The form parameters transmitted in get mode are always encoded in ISO8859-1. We need to convert it into GBK mode.
String username = request. getparameter ("username ");
System. Out. println (username );
// Transcoding, first obtain the original binary byte array
Byte [] DATA = username. getbytes ("ISO8859-1 ");
// Construct a new string based on the new character set
Username = new string (data, "GBK ");
Summary:
All pages (except for the final get garbled problem) are encoded in a uniform (GBK or UTF-8), there will be no garbled problem.

 

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.