TOMCAT7 Project migrated to Tomcat8 Chinese garbled problem

Source: Internet
Author: User

I am going to start using TOMCAT8, first solve the problem of Chinese garbled, in solving other problems!
Personal Recommendation: Modify the Server.xml way
For the SPRINGMVC, I'll add the question later.

1. Description of the problem

After the Tomcat 7 project switches to Tomcat 8, garbled characters appear. Whether Google or Baidu, most of the solution is Server.xml set uriencoding= "UTF-8", this configuration in order to solve the GET request of the Chinese garbled problem. The configuration is correct for Tomcat 7, but it is not valid for "Tomcat 7 normal, switch to Tomcat 8" garbled.

2. Solution [personal dislike]

TOMCAT8 the Server.xml Configuration Connector node adds properties uriencoding= "Iso-8859-1".
Reference: http://tomcat.apache.org/migration-8.html

3. Official Document Interpretation

Https://tomcat.apache.org/tomcat-7.0-doc/config/http.html

Uriencoding
This specifies the character encoding used to decode the URI of bytes, after%xx decoding the URL. If not specified, iso-8859-1 'll be used.

Https://tomcat.apache.org/tomcat-8.0-doc/config/http.html

Uriencoding
This specifies the character encoding used to decode the URI of bytes, after%xx decoding the URL. If not specified, UTF-8 'll be used unless the Org.apache.catalina.STRICT_SERVLET_COMPLIANCE system property was set to TR UE in which case iso-8859-1 would be used.

TOMCAT7 default encoding for URIs is iso-8859-1
Tomcat8 default encoding for URIs is UTF-8

4. About the coding problem 4.1 Tomcat7 This URI default encoding brings a lot of problems, this should be common:
 
   
  
  1. new String(value.getBytes("ISO-8859-1"), param);

If the Server.xml is configured on uriencoding= "UTF-8" it is not required. Then the project is migrated directly to the TOMCAT8, without modifying the server.xml, or adding uriencoding= "UTF-8" is not a problem.

4.2 Tomcat8 is not because of the developer server transcoding trouble, the URI default encoding changed to "UTF-8".

For the TOMCAT8 development project, it is much simpler and does not require the above-mentioned cumbersome code. But the project migrated from TOMCAT7, either, remove the code above, or server.xml add uriencoding= "iso-8859-1", wasting Tomcat8 a kindness.

4.3 Of course, by judging whether the parameter value is garbled, encoding is also very good
  
 
  1. import java.util.regex.Matcher;
  2. import java.util.regex.Pattern;
  3. public class ChineseUtill {
  4. private static boolean isChinese(char c) {
  5. Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
  6. if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
  7. || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
  8. || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
  9. || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
  10. || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
  11. || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
  12. return true;
  13. }
  14. return false;
  15. }
  16. public static boolean isMessyCode(String strName) {
  17. Pattern p = Pattern.compile("\\s*|\t*|\r*|\n*");
  18. Matcher m = p.matcher(strName);
  19. String after = m.replaceAll("");
  20. String temp = after.replaceAll("\\p{P}", "");
  21. char[] ch = temp.trim().toCharArray();
  22. float chLength = 0 ;
  23. float count = 0;
  24. for (int i = 0; i < ch.length; i++) {
  25. char c = ch[i];
  26. if (!Character.isLetterOrDigit(c)) {
  27. if (!isChinese(c)) {
  28. count = count + 1;
  29. }
  30. chLength++;
  31. }
  32. }
  33. float result = count / chLength ;
  34. if (result > 0.4) {
  35. return true;
  36. } else {
  37. return false;
  38. }
  39. }
  40. public static String toChinese(String msg){
  41. if(isMessyCode(msg)){
  42. try {
  43. return new String(msg.getBytes("ISO8859-1"), "UTF-8");
  44. } catch (Exception e) {
  45. }
  46. }
  47. return msg ;
  48. }
  49. }


From for notes (Wiz)

TOMCAT7 Project migrated to Tomcat8 Chinese garbled problem

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.