A solution for displaying garbled characters in HTML files in a Java Web project in a browser
When I was working on a Java Web project today, Chinese characters in html are garbled in the browser. The possible causes are as follows:
(1) There is a default encoding method in html file attributes. If its settings conflict with the content charset attribute settings in html documents, garbled characters are displayed.
(2) It is related to the browser. If the html encoding method is different from the default encoding method of the browser, garbled characters will appear.
View my experiment directly below:
(1) html file properties and content charset are set to UTF-8, but the default browser is GBK encoding, display garbled. I tried both Google Chrome and sogou, and the results were garbled.
Encoding of html file attributes: In MyEclipse, right-click the html file and choose Properties> Resource
The encoding format of the html file and the content charset attribute settings in the html document
The result is garbled:
(2) The default settings of the html file, content charset attribute is set to UTF-8, the results also show garbled
(3) html file properties are set to UTF-8, but content charset is set to GBK, the two conflict, the display is garbled:
(4) The html property is set to GBK, the content charset in the document is also set to UTF-8, The results show normal
Normal Results
Conclusion: If Chinese characters in the html document are garbled in the browser, find the cause from two aspects: (1) Conflict between the attribute encoding settings of html and the content charset settings (2) incompatible with the browser encoding format