Chinese garbled characters caused by JSP include HTML files

Source: Internet
Author: User

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.)


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.