Summary of Chinese garbled characters

Source: Internet
Author: User

Tag: Garbled

1. servlet
1) encode the received content (parameters)
Request. setcharacterencodeing (string charset );

2) encode the forwarded content to be output
Response. setcontexttype ("text/html; charset = UTF-8 ");

Ii. jsp
<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "UTF-8" %>

Iii. js
1. parameter passing page
JavaScript code:
<SCRIPT type = "text/JavaScript"> // <! [CDATA [
Function send (){
VaR url = "test01.html ";
VaR username = $ ("# username" ).html ();
Window. Open (encodeuri (URL + "? Username = "+ username ));}
//]>
</SCRIPT>
2. Receiving parameters page: test02.html
<SCRIPT>
VaR urlinfo = Window. Location. href; // retrieve the URL
VaR username = urlinfo. Split ("?") [1]. Split ("=") [1]; // The number of shards after the split URL is "="
Users (includomainusernamedomain.html .html (decodeuri (username ));
</SCRIPT>
Ii. How to obtain the URL "?" Then, the "=" parameter value:
A. First, use window. Location. href to get all URL values.
B. Use split to intercept "?" All
C. Split ("?") The number in [1] after it is calculated from 0 by default.

Differences between escape, Unescape, encodeuri, and encodeuricomponent:
1. When passing parameters, encodeuricomponent. Otherwise, the URL is easily marked "#","?"," And other sensitive symbols.
2. Used for URL jump, encoding using encodeuri, and decoding using decodeuri.

3. escape () only converts ASCII characters other than 0-to % u, if you want to use more characters such as the UTF-8 character library, you must use encodeuricomponent () or encodeuri () to convert the code to % NN. Otherwise, escape, encodeuri and EN codeuricomponent have the same encoding results. Therefore, to globally unify processes, use encodeuricom ponent () or encodeuri () instead of escape!


Iv. Struts

Set the constant: <contanst name = "struts. i18n. ecording" value = "UTF-8">


V. Spring
When you submit data to the server through a form, a typical problem is Chinese garbled characters. Although all of our JSP files and page encoding formats use UTF-8, but this problem still appears. The solution is simple. You only need to configure a spring encoding and conversion filter in Web. xml.
Used for development in a non-struts2 framework:
Org. springframework. Web. Filter. characterencodingfilter
<Filter>
<Filter-Name> characterencodingfilter </filter-Name>
<Filter-class>
Org. springframework. Web. Filter. characterencodingfilter
</Filter-class>
<Init-param>
<Param-Name> encoding </param-Name>
<Param-value> UTF-8 </param-value>
</Init-param>
<Init-param>
<Param-Name> forceencoding </param-Name>
<Param-value> true </param-value>
</Init-param>
</Filter>

<Filter-mapping>
<Filter-Name> characterencodingfilter </filter-Name>
<URL-pattern>/* </url-pattern>

</Filter-mapping>


Vi. html
The data in the form is encoded according to the current page.
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8;">
Simulate the Content-Type of the message header so that the browser can open the page with UTF-8 encoding.
Make sure that the form is encoded with the specified character.

VII. Cookie
The cookie value can only be ASCII characters. If it is Chinese, you need to convert ChineseASCIICharacter format.
You can use the urlencoder. encode () method and urldecoder. Decode () method to perform this conversion.


Summary of Chinese garbled characters

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.