The ultimate solution for JSP Chinese garbled problem, and the ultimate solution for jsp

Source: Internet
Author: User

The ultimate solution for JSP Chinese garbled problem, and the ultimate solution for jsp

Reprinted from: http://www.cnblogs.com/beijiguangyong/

Http://www.cnblogs.com/beijiguangyong/archive/2012/03/31/2437124.html

Before introducing the methods, we should first understand the specific problems. The JSP Chinese Garbled text discussed in this blog has the following aspects: page garbled, parameter garbled, form garbled, source file garbled. Next we will solve the garbled problem one by one.

I. garbled Chinese characters on JSP pages

On the JSP page, there are two types of Chinese garbled characters: Chinese garbled characters in HTML and Chinese garbled characters dynamically output in JSP.

First look at a JSP program:

 

[Java]View plaincopyprint?
  1. <% @ Page language = "java" import = "java. util. *" %>
  2. <Html>
  3. <Head>
  4. <Title> Chinese display example </title>
  5. </Head>
  6. <Body>
  7. This is an example of Chinese display:
  8. <%
  9. String str = "Chinese ";
  10. Out. print (str );
  11. %>
  12. </Body>
  13. </Html>

 

 

The above JSP program seems to display a few Chinese sentences on the page and the title is also Chinese. Display in the browser after running

 

The reason is that the Code displayed on the page is not specified in JSP. The solution to eliminating Garbled text is simple. In the above Code, the page command is changed to the following:

 

[Java]View plaincopyprint?
  1. <% @ Page language = "java" import = "java. util. *" contentType = "text/html; charset = GB2312" %>
  2. <Html>
  3. <Head>
  4. <Title> Chinese display example </title>
  5. </Head>
  6. <Body>
  7. This is an example of Chinese display:
  8. <%
  9. String str = "Chinese ";
  10. Out. print (str );
  11. %>
  12. </Body>
  13. </Html>

 

 

Run Garbled text again. The principle is to specify the encoding to GB2312 for the page. The page will be displayed according to the encoding, so the Garbled text disappears.

Ii. garbled Chinese characters of URL passing Parameters

In general, garbled characters may occur if the parameters passed when the get method is used to submit a form.

The following is an example Program

 

[Java]View plaincopyprint?
  1. <% @ Page language = "java" import = "java. util. *" contentType = "text/html; charset = gb2312" %>
  2. <Html>
  3. <Head>
  4. <Title> Chinese processing example of URL Transfer Parameters </title>
  5. </Head>
  6. <%
  7. String param = request. getParameter ("param ");
  8. %>
  9. <Body>
  10. <A href = "URLCharset. jsp? Param = 'Chinese' "> click this link </a> <br>
  11. The parameter you submitted is: <% = param %>
  12. </Body>
  13. </Html>

 

 

The above JSP program function is to pass a parameter to itself through a URL link. this parameter is a Chinese string, and the running effect of this program is as follows:

 

The Processing Method for garbled Chinese parameters transmitted by URLs is special. The problem cannot be solved only by converting the Chinese string or setting the display encoding of the JSP page, you must modify the configuration file of the Tomcat server to solve the problem. Modify the server. xml configuration file in the conf directory of Tomcat. The code after modification is as follows:

 

[Html]View plaincopyprint?
  1. <Connector port = "8080" protocol = "HTTP/1.1" URIEncoding = "gb2312"
  2. ConnectionTimeout = "20000"
  3. RedirectPort = "8443" type = "regxph" text = "yourobjectname"/>

 

 

Add URI encoding in the original code and set URIEncoding = "gb2312". Restart the Tomcat server to get the correct page. The principle is similar to the above situation, that is, to specify the encoding type for the program, and then the display is normal.

3. garbled Chinese Characters in Form submission

You can use the request. getParameter ("") method to obtain the data of the form. However, garbled characters may appear when the Chinese data in the form is displayed.

The sample code is as follows:

 

[Java]View plaincopyprint?
  1. <% @ Page language = "java" import = "java. util. *" contentType = "text/html; charset = gb2312" %>
  2. <Html>
  3. <Head>
  4. <Title> Form Chinese processing example </title>
  5. </Head>
  6. <Body>
  7. <Font size = "2">
  8. The following is the form content:
  9. <Form action = "AcceptFormCharset. jsp" method = "post">
  10. UserName: <input type = "text" name = "userName" size = "10"/>
  11. Password: <input type = "password" name = "password" size = "10"/>
  12. <Input type = "submit" value = "submit">
  13. </Form>
  14. </Font>
  15. </Body>
  16. </Html>

 

 

In the above form, we want to increase the price of two items on the AcceptFormCharset page. The following is the content of AcceptFormCharset. jsp:

 

[Java]View plaincopyprint?
  1. <% @ Page language = "java" import = "java. util .*"
  2. ContentType = "text/html; charset = gb2312" %>
  3. <Html>
  4. <Head>
  5. <Title> Form Chinese garbled characters </title>
  6. </Head>
  7. <Body>
  8. <Font size = "2"> The following is the form data obtained by the request after the form is submitted: <br>
  9. <%
  10. String userName = request. getParameter ("userName ");
  11. String password = request. getParameter ("password ");
  12. Out. println ("form input userName value:" + userName + "<br> ");
  13. Out. println ("input password in the form:" + password + "<br> ");
  14. %>
  15. </Font>
  16. </Body>
  17. </Html>

 

 

In the above program, if the form input does not contain Chinese characters, the results will be displayed normally when the input data contains Chinese characters.

 

The reason for this is that the form submitted by the post method in Tomcat uses the default encoding for the ISO-8859-1, and this encoding format does not support Chinese characters. We can solve this problem by converting the encoding format. The changes to the AcceptFromCharset page are as follows:

 

[Java]View plaincopyprint?
  1. <% @ Page language = "java" import = "java. util .*"
  2. ContentType = "text/html; charset = gb2312" %>
  3. <Html>
  4. <Head>
  5. <Title> Form Chinese garbled characters </title>
  6. </Head>
  7. <Body>
  8. <Font size = "2"> The following is the form data obtained by the request after the form is submitted: <br>
  9. <%
  10. String userName = request. getParameter ("userName ");
  11. String password = request. getParameter ("password ");
  12. Out. println ("form input userName value:" + new String (userName. getBytes ("ISO-8859-1"), "gb2312") + "<br> ");
  13. Out. println ("form input password value:" + new String (password. getBytes ("ISO-8859-1"), "gb2312") + "<br> ");
  14. %>
  15. </Font>
  16. </Body>
  17. </Html>

 

 

After such conversion and encoding, all Chinese input can be taken out normally using the request object. In the above program, the fourth line and the fifth line are the key to the conversion of the encoding format, first from the ISO-8859-1 format string to retrieve the byte content, then, reconstructs a new string in GB2312 encoding format. In this way, the normal value and display of Chinese fade-In are supported. After improvement, the program runs as follows:

 

After the above encoding format change, the Chinese Input garbled characters in the form have been resolved. However, if there are more than two input items in the form above, it is very troublesome to encode and convert each input item. This is a well-known filter. For more information about the content, see the author's article.

4. garbled Chinese Characters in JSP files in Eclipse

In Eclipse or MyEclipse, because the default JSP encoding format is ISO-8859-1, garbled characters appear when opening JSP files edited by other editors,

 

To solve this problem, you only need to change the default code of JSP in Eclipse or MyEclipse. The modification is made (My MyEclipse version is 11)

 

PS

In Eclipse or MyEclipse, JSP files are encoded as ISO-8859-1 by default, so if there is a Chinese character in the JSP code, it cannot be saved, such as the following code

 

[Java]View plaincopyprint?
  1. <% @ Page language = "java" import = "java. util. *" %>
  2. <Html>
  3. <Head>
  4. <Title> Chinese display example </title>
  5. </Head>
  6. <Body>
  7. This is an example of Chinese display:
  8. <%
  9. String str = "Chinese ";
  10. Out. print (str );
  11. %>
  12. </Body>
  13. </Html>

 

 

After the modification, the following message is displayed when you save the modification:

 

The cause of this prompt is that the JSP source file contains Chinese characters that are not recognized by ISO = 8859-1 encoding. To solve this problem, declare the page encoding format on the JSP page. The declared code is as follows:

 

[Java]View plaincopyprint?
  1. <% @ Page language = "java" import = "java. util. *" pageEncoding = "GB2312" %>
  2. <Html>
  3. <Head>
  4. <Title> Chinese display example </title>
  5. </Head>
  6. <Body>
  7. This is an example of Chinese display:
  8. <%
  9. String str = "Chinese ";
  10. Out. print (str );
  11. %>
  12. </Body>
  13. </Html>

 

 

In the first line, pageEncoding = "gb2312" indicates that the JSP page encoding adopts GB2312, so that the source file of JSP can be properly saved.

 

When you encounter a problem, first analyze the cause of the problem. You can solve the problem only by knowing the cause. Learning and analyzing the source of the problem is far more important than solving the problem.

The cause of the garbled code problem is that the encoding of the Program (Eclipse or browser) is not consistent with that of the programmer, (just like you can communicate with a person who doesn't understand Chinese. Of course, he doesn't understand.) to solve this problem, you only need to tell the program the code that the programmer wants, the above methods to solve the garbled problem can be said to be a process of declaring code, that is, the ultimate solution to the garbled problem is:Transcoding.The transcoding here is either manually transferred by the programmer or declared to let the program go, that is, to communicate with people who do not understand Chinese, or to learn Chinese, you can either learn his language.

Life is programming, and programming is life !!!

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.