Does the browser display garbled characters in a web project?

When they first learn web project development, many people will encounter the problem of garbled Chinese characters. The solutions on Baidu are dazzling, and no matter how much modifications are made, the problem still cannot be solved. How are garbled codes generated? When you know why the code is garbled and understand the underlying principles, you will find that your eyes are bright. Without further ado, look at the picture below:
Insert image description here

It can be seen from the above figure

  • The encoding format used by encoding 1 and decoding 1 must be the same
  • The encoding format used by encoding 2 and decoding 2 must be the same

Note: When the above two conditions are met, there will be no garbled characters. Generally, UTF-8 (universal code) is used in these four places.

Coding settings: (according to the serial number in the picture above)

  1. form:<form id="xx" action="xx" method="xx" accept-charset = "UTF-8">

  2. Server encoding format (request):
    Tomcat uses different ways to process encoding for information submitted by the GET and POST methods. For POST requests, Tomcat will use the encoding set by the request.setCharacterEncoding method. If it is not set, it will use Default encoding. The GET request is different. Tomcat uniformly uses Tomcat's default encoding for GET requests.
    The default encoding method for Tomcat 7 and previous versions is IOS-8859-1 . Starting from Tomcat 8, UTF-8 is used by default.

    1. Receive get method
    Method 1: (Know what character set encoding is used) Decode first, then encode
    String str = new String(getParameter("message").getBytes("ISO-8859-1"),"UTF-8");

    Method 2: Modify the tomcat configuration file (not recommended)
    <Connector URIEncoding="utf-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>

    2. Receive post method
    Method 1: request.setCharacterEncoding("UTF-8") ;(It is valid before getting parameters)

    Method 2: You need to configure the filter in web.xml

<!-- post乱码过虑器 -->
   <filter>
       <filter-name>CharacterEncodingFilter</filter-name>
       <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
       <init-param>
           <param-name>encoding</param-name>
           <param-value>utf-8</param-value>
       </init-param>
   </filter>
   <filter-mapping>
       <filter-name>CharacterEncodingFilter</filter-name>
       <url-pattern>/*</url-pattern>
   </filter-mapping>
  1. response:
    Method 1: response.setCharacterEncoding("UTF-8");(can only be used to set the encoding used in the out output stream)

    Method 2: response.setContentType("text/html;charset=UTF-8");(Set the encoding method of characters in the out output stream, and also set the encoding method used by the browser to decode these characters after receiving them)

  2. The browser displays:
    Add jsp or html to the page header <meta content="text/html; charset=utf-8" />"
    . Open the development mode in the browser and use document.charset in the console to view the encoding format of the current page.
    Insert image description here

Summary: If the encoding methods in the above four places are the same, garbled characters will not occur.

Solve the garbled characters in the eclipse editor, click to view

Guess you like

Origin blog.csdn.net/weixin_40307206/article/details/103081479