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:
- Specifies the character set as a UTF-8 when developing and compiling code. Both JBuilder and eclipse can be set in project properties.
- 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.
- Declare in the JSP header: <% @ page contenttype = "text/html; charset = UTF-8" %>.
- In jsp html code, declare the UTF-8:
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
- 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
- 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.