Charset in J2EE Web Application

Source: Internet
Author: User
Tags file upload tomcat
Running environment: Win2K Pro Japanese version, SUN J2SDK 1.4.2 _ 04, Tomcat 4.1.27, JSPs
Because in Tomcat. the data obtained by The getParameter (String) method is a Unicode String corresponding to "ISO8859_1" (I guess the entire process should be like this: assume that the HTML Encoding is "Shift_JIS ", then, IE converts the values of the input controls encoded by "Shift_JIS" in form to the data stream encoded by "ISO8859_1" and sends it to Tomcat, then Tomcat converts the data encoded with "ISO8859_1" to the Unicode data corresponding to "ISO8859_1" and passes the data to our JSPs through the request object. After JSPs completes its own processing, finally, the Unicode corresponding to "Shift_JIS" is passed to Tomcat through the response object, and then Tomcat converts the data to the "ISO8859_1" data stream and sends it back to the IE of the client, then IE sends the received "ISO8859_1" encoded data Convert to "Shift_JIS" encoded data and display it as an HTML page. The reason for communication between the client and the server is to use "ISO8859_1" encoding, it may be to prevent the high 8-bit dual-byte characters from being ignored by some network servers, resulting in data loss, because "ISO8859_1" encoding only has eight characters long, while "Shift_JIS" and other double-byte character sets have a high length of eight characters ), therefore, after we obtain the request data in JSPs, it is generally necessary to convert the data without garbled characters. We need to perform a conversion similar to the following in JSPs:
String reqParamA = new String (request. getParameter ("txt_a"). getBytes ("ISO8859_1"), "Shift_JIS");
However, when outputting data to the client, you do not need to convert the data. You can directly output the Unicode string corresponding to "Shift_JIS" to the client.
The above describes the typical process of sending a request from the client to the server and sending a response to the client. The client receives the response and displays the HTML page. However, some exceptions need to be noted:
By calling response. sendRedirect (str_url), when the server directly redirects to another URL. Assume that str_url contains queryString (such as someUrl? ParamA = "[string containing double-byte characters]" & paramB = "[string containing double-byte characters]"...), the str_url must be converted like the following, otherwise the request data obtained by the JSPs mapped to the destination URL will be garbled (as mentioned above, the expected request data should be the Unicode string corresponding to "ISO8859_1 ):
Response. sendRediret (str_url.getBytes ("Shift_JIS"), "ISO8859_1");
File upload. The data stream obtained by JSPs on the server is encoded as the Unicode data stream corresponding to the character set of the file. For example, in Win2K pro, a .csv file is encoded as "MS932". When it is uploaded to the server, the data stream obtained by JSPs is the Unicode data stream corresponding to "MS932.

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.