Summary of jsp project deployment in Apache Tomcat 5.5 -- Solution to jsp garbled Problem

Source: Internet
Author: User
Tags dreamweaver apache tomcat
This section mainly summarizes the solutions to jsp garbled code problems.

1. The most basic garbled problem.
This garbled problem is the simplest garbled problem. Generally, it appears new. It is the garbled code caused by inconsistent page codes.
<% @ Page language = "java" pageEncoding = "UTF-8" %>
<% @ Page contentType = "text/html; charset = iso8859-1" %>
<Html>
<Head>
<Title> Chinese </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
</Head>
</Head>
<Body>
I'm a good guy.
</Body>
</Html>
Encoding in three places.
The encoding format in the first place is the storage format of jsp files. Ecljpse saves files according to the encoding format. Compile the jsp file, including Chinese characters.
The second part is the decoding format. Because the file stored as a UTF-8 is decoded as a iso8859-1, such as a Chinese certainly garbled. That is, it must be consistent. The row in the second part does not exist. By default, it is also the encoding format that uses the iso8859-1. If this line does not exist, "I am a good person" will also be garbled. Must be consistent.
The third encoding is to control the browser's decoding method. If all the preceding decoding operations are consistent and correct, the encoding format does not matter. Some web pages are garbled because the browser cannot determine which encoding format to use. Because the page is sometimes embedded into the page, the browser obfuscated the encoding format. Garbled characters appear.

2. garbled characters received after the form is submitted in Post Mode
This is also a common problem. This garbled code is tomcat's internal encoding format iso8859-1 in disorder, that is to say, when the post is submitted, if there is no set to submit the encoding format, it will be submitted in iso8859-1 mode, the accepted jsp is accepted in UTF-8 format. Cause garbled characters. For this reason, there are several solutions and comparison below.
A. encode and convert Parameters
String str = new String (request. getParameter ("something"). getBytes ("ISO-8859-1"), "UTF-8"); in this way, each parameter must be transcoded. Very troublesome. However, Chinese characters can be obtained.
B. At the beginning of the request page, execute the request encoding code, request. setCharacterEncoding ("UTF-8"), set the character set of the submitted content to the UTF-8. In this way, the page that accepts this parameter does not have to be transcoded. Direct use
String str = request. getParameter ("something. However, this statement must be executed on each page. This method also has an effect on post submission. The enctype = "multipart/form-data" for get submission and file upload is invalid. Later, we will explain the garbled characters of the two.
C. To avoid writing request. setCharacterEncoding ("UTF-8") per page, we recommend that you use a filter for all jsp
Encoding. There are many examples on the Internet. You can check it for yourself.

3. garbled processing for form get submission.
If you use the get method to submit Chinese, the page that receives the parameter will also be garbled, this garbled cause is also caused by tomcat's internal encoding format iso8859-1. Tomcat will get the default encoding method of iso8859-1 to encode Chinese characters, encoding appended to the url, resulting in the acceptance of the page parameters are garbled /,.
Solution:
A. Use the first method in the preceding example to decode the received character and then transcode it.
B. Get goes through url submission, and the iso8859-1 encoding has been performed before entering the url. To affect the encoding, add useBodyEncodingForURI = "true" to the Connector node of server. xml"
Attribute configuration, you can control tomcat to get the way of Chinese character encoding, the above property control get submit is also set by request. setCharacterEncoding ("UTF-8") encoding format. So the automatic encoding is UTF-8, and the page can be accepted normally. But I think the real encoding process is that tomcat needs
<Connection port = "8080"
MaxThreads = "150" minSpareThreads = "25" maxSpareThreads = "75"
EnableLookups = "false" redirectPort = "8443" acceptCount = "100"
Debug = "0" connectionTimeout = "20000" useBodyEncodingForURI = "true"
DisableUploadTimeout = "true" URIEncoding = "UTF-8"/>
The URIEncoding = "UTF-8" set in it is coded again, but since it has been encoded as UTF-8, the encoding will not change. If the encoding is obtained from the url, the accept page is decoded Based on URIEncoding = "UTF-8.

4. Solve the garbled characters when uploading files
When uploading files, the form is set to enctype = "multipart/form-data ". This method submits files in streaming mode. If you use the apach Upload Component, you will find many garbled characters. This is because of a bug in the early commons-fileupload.jar of apach, Which is decoded after the Chinese characters are taken out, because this method is submitted, the encoding is automatically used by the tomcat default encoding format iso-8859-1. But the garbled problem is: periods, commas, and other special characters become garbled. If the number of Chinese characters is odd, garbled characters will occur, and even numbers will be parsed normally.
Work und: downloading the jar version of The commons-fileupload-1.1.1.jar has fixed these bugs.
But the extracted characters still need to be transcoded from the iso8859-1 to UTF-8. All Chinese characters and characters can be obtained normally.

5. Java code for url requests, garbled parameters are accepted
The url encoding format depends on the URIEncoding = "UTF-8" mentioned above ". If this encoding format is set, it means that all the Chinese character parameters to the url must be encoded. Otherwise, the obtained value of the Chinese character parameter is garbled, for example, a link Response. sendDerect ("/a. jsp? Name = Zhang Dawei "), and in a. jsp, use String name = request. getParameter (" name ") directly. garbled characters are obtained. Because UTF-8 is required, the conversion should be written as follows: Response. sendDerect ("/a. jsp? Name = URLEncode. encode ("Zhang Dawei", "UTF-8. What if you don't set this parameter URIEncoding = "UTF-8? If not set, the default encoding format iso8859-1 is used. The problem arises again. The first is that if the number of parameter values is an odd number, it can be parsed normally. If the number is an even number, the final character is garbled. In addition, if the last character is in English, it can be parsed normally, but the Chinese Punctuation Marks are still garbled. If your parameter does not contain Chinese Punctuation Marks, you can add an English symbol at the end of the parameter value to solve the garbled problem. After obtaining the parameter, remove the last symbol. It can also be used together.

6. The script code contains garbled parameters for url requests.
The script also controls page redirection, also involves parameters, and accepts the page parsing parameter. If the Chinese character parameter is not subject to the encoding specified by URIEncoding = "UTF-8", the Chinese characters accepted by the page are garbled. It is troublesome for the script to process the encoding. You must have the corresponding encoding script file, and then call the method in the script to encode the Chinese characters.

7. jsp garbled characters in MyEclipse
For an existing project, the storage format of Jsp files may be UTF-8. If the newly installed eclipse is enabled, the encoding formats used by default are iso8859-1. As a result, Chinese characters in jsp are garbled. This Garbled text is easy to solve. Go to eclipse3.1's preference settings to find general-> edidor and set it to UTF-8 for your file opening. Eclipse will automatically open it again in the new encoding format. The Chinese characters are displayed normally.

8. garbled characters occur when html pages are opened in eclipse.
Most pages are created by dreamweaver, and their storage formats are different from those identified by eclipse.
In general, create a new jsp in eclipse and copy the page content from dreamweaver and paste it to jsp.

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.