Resolve Tomcat Chinese problems in principle

Source: Internet
Author: User

Many open-source software have problems with internationalization and localization. In fact, internationalization is not so complicated, but some companies always want to increase their market share by rejecting international standards, so it gradually leads to the current situation. Most open-source software supports UTF Encoding, followed by other encoding. (By gashero) Of course, some of the software's preferred choice is ISO-8859-1 encoding, or even ASCII encoding. Well, let's talk about coding first, Because tomcat (a very useful JSP running platform) is also a non-tuned software.

Common Chinese encodings include GB2312, GBK, and GB18030. Of course, they do not include traditional Chinese. These encodings all represent encoding rather than display encoding. Nowadays, more and more software uses the display encoding or processing encoding at its core, which is Unicode encoding. Unicode encoding uses 2 bytes to represent almost all the text symbols in the world. It is no longer suitable for internal internationalization of the software.

For commercial purposes, some zookeeper companies block all the Unicode encoding APIs in the kernel and only provide local encoding. (By gashero) for example, company M $ mainly uses GBK encoding in Chinese software.

Another problem is the very old software written in C/C ++. The End mark of the string is. If Unicode is used, some strings will be truncated for no reason. So the UTF-8 came into being, this variable length encoding can reduce the size of the string, but also can prevent the phase and transfer Unicode encoding.

The traditional English encoding is the most common one. One is ASCII. This character set is 0 at a high level and uses 7 bits to represent data. The other is the ISO-8859-1. Each character is 1 byte and represents one character in 8 bits.

Tomcat (a very useful JSP operating platform) built-in encoding is the use of the ISO-8859-1. This is also the most critical sentence in this article.

From this we can see that the data submitted by the web page needs to be decoded from the ISO-8859-1, the following is an example of obtaining the submit parameter number.

String number = new String (request. getParameter ("number"). getBytes ("ISO-8859-1"), "UTF-8 ");

This sentence successfully realizes the conversion from ISO-8859-1 to UTF-8 encoding.

Experienced readers should have encountered an error when passing parameters between webpages through the <jsp (preferred for SUN Enterprise Applications): param> tag. (By gashero) all received strings are question marks. In fact, this is also caused by the internal code of tomcat (a very useful JSP running platform. If you can adapt to the internal code of tomcat (a very useful JSP running platform), you can pass Chinese strings.

For example, internal redirection between pages:

<? Xml (standardization is getting closer and closer): namespace prefix = jsp (preferred for SUN Enterprise Applications)/> <jsp (preferred for SUN Enterprise Applications): forward page = "index. jsp (preferred for SUN Enterprise Applications) ">

<Jsp (preferred for SUN Enterprise Applications): param name = "name" value = "<% = new String (" % Chinese parameter ?. GetBytes (? UTF-8 ?),? ISO-8859-1?)> "/>

</Jsp (preferred for SUN Enterprise Applications): forward>

When receiving parameters on the target page, you also need to convert from ISO-8859-1 encoding to UTF-8 encoding again. In this way, Chinese parameters are passed between pages.

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.