FAQs about Chinese JSP pages in Tomcat 4.0 and Tomcat 4.1

Source: Internet
Author: User

Currently, Tomcat, as an outstanding open-source JSP server, has been widely used in JSP development. however, as a software developed by an English-speaking Company, garbled characters are inevitable in Chinese environments. here we will summarize the common Chinese problems in Tomcat 4.0 and Tomcat 4.1 and their solutions. these methods have been tested in Windows 98 + JDK 1.3.1 and Windows 2000 + JDK 1.3.1. in addition, there is a Web http://www-900.ibm.com/developerWorks/cn/java/jsp_dbcsz/index.shtml on IBM's website also discussed this problem.

First, for convenience of discussion, some convenient tools and methods are listed here to facilitate our discussion. These methods are as follows:

 
 
  1. Public String toChi (String input ){
  2. Try {
  3. Byte []Bytes=Input. GetBytes ("ISO8859-1 ");
  4. Return new String (bytes );
  5. } Catch (Exception ex ){
  6. }
  7. Return null;
  8. }
  9. // Perform URL encoding for the specified character
  10. Public String encode (String value ){
  11. If (isEmpty (value) return "";
  12. Return java.net. URLEncoder. encode (value );
  13. }
  14. // Perform URL Decoding for the specified character
  15. Public String decode (String value ){
  16. If (isEmpty (value) return "";
  17. Return java.net. URLDecoder. decode (value );
  18. }

Question 1. How can the Chinese characters on the JSP page displayed in the browser become
The possible reasons are as follows: the character set of the page is not specified on your page as Chinese. solution (applicable to Tomcat 4.0 and Tomcat 4.1) is to add the following code to the page:

 
 
  1. <%@ page contentType="text/html;charset=gb2312" %> 
  2. <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 

Problem 2. The Chinese characters of the form submitted through the POST method are displayed as garbled characters (normally under Tomcat 4.0 and Tomcat 4.1 ).
The possible cause is as follows: the string submitted by POST is ISO8859-1 encoded, as long as it is converted to the Chinese character set. The solution is as follows (applicable to Tomcat 4.1 ):

 
 
  1. // Single parameter
  2. StringResult=ToChi(Request. getParameter ("parameterName "));
  3. // Multiple parameters
  4. String values [] = request. getParameterValues (name );
  5. If (values! = Null ){
  6. For (intI=0; I< Values. length; I ++ ){
  7. Values [I] = toChi (values [I]);
  8. }
  9. }

Question 3. the Chinese characters of the forms submitted using the GET method are garbled (both Tomcat 4.0 and Tomcat 4.1 ). the possible cause is as follows: the string submitted by GET is ISO8859-1 encoded, as long as it is converted to the Chinese character set. the solution is as follows (applicable to Tomcat 4.1, Tomcat 4.0 cannot be used for page. jsp? Username = Chinese ):

 
 
  1. // Single parameter
  2. StringResult=ToChi(Request. getParameter ("parameterName "));
  3. // Multiple parameters
  4. String values [] = request. getParameterValues (name );
  5. If (values! = Null ){
  6. For (intI=0; I< Values. length; I ++ ){
  7. Values [I] = toChi (values [I]);
  8. }
  9. }

Problem 4. the Cookie cannot contain Chinese characters or the Chinese characters cannot be correctly displayed. the possible cause is as follows: Tomcat 4.0 automatically encodes cookies into ISO8859-1 storage, while Tomcat 4.1 JSP engine does not support cookies containing Chinese characters.

Solution for Tomcat 4.0:

 
 
  1. // Obtain the Cookie value in the request based on the Cookie name. If the Cookie value is null, "" is returned ""
  2. Public String getCookieValue (HttpServletRequest request, String name ){
  3. Cookie []Cookies=Request. GetCookies ();
  4. If (Cookies= Null) return "";
  5. For (intI=0; I< Cookies. length; I ++ ){
  6. CookieCookie=Cookies[I];
  7. If (cookie. getName (). equals (name )){
  8. // URL deserialization is required for Chinese characters in the Cookie. Applicable version: Tomcat 4.0
  9. Return decode (cookie. getValue ());
  10. }
  11. }
  12. // A cookie might not return a null value, may return ""
  13. Return "";
  14. }

Solution for Tomcat 4.1:

 
 
  1. // Write a Cookie containing Chinese Characters
  2. Response. addCookie (new Cookie ("cookieName", encode ("Chinese character ")));
  3. // Method for obtaining the Cookie value (same as the solution of Tomcat 4.0)
  4. Public String getCookieValue (HttpServletRequest request, String name ){
  5. Cookie []Cookies=Request. GetCookies ();
  6. If (Cookies= Null) return "";
  7. For (intI=0; I< Cookies. length; I ++ ){
  8. CookieCookie=Cookies[I];
  9. If (cookie. getName (). equals (name )){
  10. // URL deserialization is required for Chinese characters in the Cookie. Applicable version: Tomcat 4.0
  11. Return decode (cookie. getValue ());
  12. }
  13. }
  14. // A cookie might not return a null value, may return ""
  15. Return "";
  16. }

Question 5: GET requests under Tomcat 4.0 (for example, page. jsp? Username = Chinese) the original value cannot be returned. Cause: it is related to the Tomcat engine. No matter whether or not the internal code is converted, the original value cannot be returned, but there is an alternative method, as shown below:
Change the URL address to "page. jsp? Username = "+ encode (" Chinese ") and then use the following code to retrieve parameters:

 
 
  1. // Single parameter
  2. StringResult=ToChi(Request. getParameter ("parameterName "));
  1. Brief Introduction to advanced operations on JSP Databases
  2. Interactive use of JSP data and JavaScript data
  3. Functions and principles of several encodings in JSP and Servlet
  4. How to solve the problem of garbled JSP page display
  5. Connect to various databases using JDBC in JSP

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.