[-Post and repost-] solve Chinese garbled characters such as JSP, struts, tapestry, and Hibernate

Source: Internet
Author: User
Define the unified coding of the Java/J2EE system as a UTF-8. UTF-8 coding is a way of coding compatible with all languages, the only trouble is to find all the entrances and exits of the application system, and then use the UTF-8 to "ligation" it.

A J2EE application system requires the following steps:

  1. Specifies the character set as a UTF-8 when developing and compiling code. Both JBuilder and eclipse can be set in project properties.
  2. Using a filter, if all requests go through a servlet control distributor, use the servlet filter to execute the statement and convert all requests from the browser to the UTF-8, because the request packet sent by the browser is encoded according to the operating system of the browser, it may be encoded in various forms. Key sentence:
    Request. setcharacterencoding ("UTF-8 ").
    The source code of this filter is available on the Internet. com. jdon. util. setcharacterencodingfilter is in the jdon framework source code.
    You need to configure web. XML to activate the filter.
  3. Declare in the JSP header: <% @ page contenttype = "text/html; charset = UTF-8" %>.
  4. In jsp html code, declare the UTF-8:
    <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
  5. Set the database connection method to a UTF-8. For example, when connecting to MySQL, configure the URL as follows:
    JDBC: mysql: /// localhost: 3306/test? Useunicode = true & amp; characterencoding = UTF-8
    Note that the above writing is JBoss mysql-ds.xml writing, thanks to the prompts, in Tomcat & amp; To write & can. Typically, other databases can set UTF-8 through management settings
  6. Other UTF-8 can be set when encoding is set when interacting with the outside world, such as reading files and operating XML.

I used to adopt this principle in JSP/servlet. Later, when I used struts, tapestry, EJB, hibernate, jdon and other frameworks, I was never troubled by garbled characters. It can be said that they were suitable for various architectures. I hope this solution will be shared by more beginners to reduce the first obstacle of Java/J2EE and avoid Chinese problems in the new technical architecture due to some temporary solutions.

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.