Recently I have seen many forums that there are only three points:
1. In a static webpage, add <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"
/>. But the problem is that this only modifies the content encoding that is passed to the browser after the HTML file is compiled, and still does not modify its page encoding. In many cases, this cannot solve the problem;
2. Add <% @ page Language = "Java" contenttype = "text/html; charset = UTF-8" pageencoding = "UTF-8" %> In the JSP file.
When you include HTML, the characters in JSP are not garbled, which obviously does not work.
3. Modify the Tomcat XML file. It turns out that atat7.0 does not support this method.
I have the following several ways that I think can fundamentally solve the problem and hope readers can learn from them.
1. fundamentally solve the page encoding problem. Open the HTML file or JSP file with TXT. Click Save As, select the encoding format as UTF-8, OK. At this time, your problem may have been solved. (This method is not feasible for large projects, but it can help beginners ).
2. When the form method is post, the Chinese characters obtained by the request may be garbled in many cases, we first encode the obtained string with a UTF-8, then store it in a byte array, and then convert this array into a string. For example:
String username = request. getparameter ("username ");
Byte busername [] = username. getbytes ("UTF-8 ");
Username = new string (busername );
3. Call the following method once each time.
<%!
Public String tochinese (string Str) throws java. Io. unsupportedencodingexception
{
Byte B [] = Str. getbytes ("UTF-8 ");
STR = new string (B );
Return STR;
}
%>
(For details, see JSP dynamic website development edited by Zhang Yinhe.)