Chinese problems in Tomcat + Jsp environment

Source: Internet
Author: User
Tags character set tomcat
Abstract: There have been a lot of discussions about the problems that Java applications have encountered in processing Chinese characters. Unlike most discussions, this article will discuss the problems in Java when processing Chinese characters from the perspective of the input and output of Chinese characters.
Although there are many discussions about Java's problems in processing Chinese characters, Java technology involves a wide range of content (J2EE includes more than a dozen related technologies), and there are many technical vendors, java-oriented Web servers, application servers, and JDBC database drivers do not have official standards, therefore, Java applications have inherent problems in processing Chinese characters. In addition, some platform-related problems may occur with the selection of servers and drivers. That is to say, Java code portability discounts when dealing with Chinese problems.
In general, Java's Chinese processing problems are concentrated in JSP technology applications and Java database access. This is because both JSP applications and JDBC-based database access involve the interaction between Java programs and another application system, this kind of interaction inevitably requires data interaction and parameter transmission between systems. The problem with processing Chinese characters in Java is often where the data is read and output.
Chinese notes for JSP programs
Take the JSP application of Tomcat 3.2.1 as an example. If you encounter a Chinese problem, you can use the following encoding force conversion function to convert the internal code.
Public static String toChinese (String strvalue)
{
Try {
If (strvalue = null)
Return null;
Else
{
Strvalue = new String (strvalue. getBytes ("ISO8859_1"), "GBK ");
Return strvalue;
}
} Catch (Exception e ){
Return null;
}
}
Note: Before using this function, we need to analyze why Chinese cannot be correctly output, rather than using this method to solve all Chinese processing problems. For example, if you forget to define the JSP output code as GB2312 or GBK, you cannot use this function to output Chinese characters correctly. A good habit is to define the character set to be output by the program in the first line of the file when writing every JSP page, such
<% @ Page contentType = "text/html; charset = GBK" %> or
<% @ Page contentType = "text/html; charset = GB2312" %>
For JSP versions that do not support defining output character sets, we can also make the following settings:
<META HTTP-EQUIV = "Content-Type" CONTENT = "text/html; charset = gb2312">
In addition, it should be noted that this function is used to solve the code that does not allow correct output of Chinese characters, rather than a common function used to ensure correct output of Chinese characters. The reason why Chinese characters cannot be correctly output or read is because of the character encoding and the default character set encoding (or the character set to be output by the application, which is usually the same) so before applying this function, we must determine whether the encoding of the characters we want to read or output is the same as the default character set encoding.
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.