FAQs about JSP pages on the Tomcat server

Source: Internet
Author: User

Guidance:
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:
// Convert the internal code of the data read from the form to gb2312
Public String Tochi (string input ){
Try {
Byte [] bytes = input. getbytes ("ISO8859-1 ");
Return new string (bytes );
} Catch (exception ex ){
}
Return NULL;
}
  
// Perform URL encoding for the specified character
Public String encode (string value ){
If (isempty (value) Return "";
Return java.net. urlencoder. encode (value );
}
  
// Perform URL Decoding for the specified character
Public String decode (string value ){
If (isempty (value) Return "";
Return java.net. urldecoder. Decode (value );
}
  
Question 1: Why are Chinese characters on the JSP page displayed in the browser '? '?
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:
  
  
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 ):
// Single parameter
String result = Tochi (request. getparameter ("parametername "));
// Multiple parameters
String values [] = request. getparametervalues (name );
If (values! = NULL ){
For (INT I = 0; I Values [I] = Tochi (Values [I]);
}
}
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 ):
// Single parameter
String result = Tochi (request. getparameter ("parametername "));
// Multiple parameters
String values [] = request. getparametervalues (name );
If (values! = NULL ){
For (INT I = 0; I Values [I] = Tochi (Values [I]);
}
}
Problem 4. Chinese characters or Chinese characters cannot be correctly displayed in cookies.
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:
// Obtain the cookie value in the request based on the cookie name. If the cookie value is null, "" is returned ""
Public String getcookievalue (httpservletrequest request, string name ){
Cookie [] cookies = request. getcookies ();
If (cookies = NULL) Return "";
For (INT I = 0; I Cookie = Cookies [I];
If (cookie. getname (). Equals (name )){
// URL deserialization is required for Chinese characters in the cookie. Applicable version: Tomcat 4.0
Return decode (cookie. getvalue ());
}
}
// A cookie might not return a null value, may return ""
Return "";
}
Solution for Tomcat 4.1:
// Write a cookie containing Chinese Characters
Response. addcookie (new cookie ("cookiename", encode ("Chinese character ")));
// Method for obtaining the cookie value (same as the solution of Tomcat 4.0)
Public String getcookievalue (httpservletrequest request, string name ){
Cookie [] cookies = request. getcookies ();
If (cookies = NULL) Return "";
For (INT I = 0; I Cookie = Cookies [I];
If (cookie. getname (). Equals (name )){
// URL deserialization is required for Chinese characters in the cookie. Applicable version: Tomcat 4.0
Return decode (cookie. getvalue ());
}
}
// A cookie might not return a null value, may return ""
Return "";
}
Question 5: GET requests under Tomcat 4.0 (for example, page. jsp? Username = Chinese) cannot return the original value.
Cause: it is related to the Tomcat engine. no matter whether the internal code is converted or not, the original value cannot be returned, but there is an alternative method as follows:
Change the URL address to "Page. jsp? Username = "+ encode (" Chinese ")

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.