1. html
Whether it is an independent html, or generated by other programs, such as Servlet, note that meta tags must be added between the final html and to specify the encoding of characters entered in html, such:
<Head> <meta http-equiv = "Content-Type" content = "text/html; charset = gb2312 "> <title> test GET & POST-Send </title>
2. jsp and servlet
First, the program output (such as response. writeln (String s) and accept data from the client (such as request. getParameter (String sname) encoding problem, we can use the File filter function, the specific jsp/servlet container or the function settings provided by the server, such as in Tomcat5.5.9 can be in webapps/yourAppDirectory/WEB-INF/web. the settings in xml are as follows:
<filter> <filter-name>SetCharsetEncodingFilter</filter-name> <display-name>SetCharsetEncodingFilter</display-name> <description>Set CharsetEncoding Filter</description> <filter-class>com.gg.comm.web.SetCharsetEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>gb2312</param-value> </init-param> </filter> <filter-mapping> <filter-name>SetCharsetEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
The SetCharsetEncodingFilter Class is the filter Class used to set the request and reponse character encoding. The setting statement is as follows:
request.setCharacterEncoding(targetEncoding); response.setContentType("text/html"); response.setCharacterEncoding(targetEncoding);
In addition, to solve the problem of garbled characters when passing parameters through get (url with parameters), we also need to set the encoding required for url passing parameters, in Tomcat5.5.9, you can go to $ {Tomcat_home} confserver. in xml, the settings are as follows:
<!-- URIEncoding="GBK":Force GET method String(Chinese) can be transferd properly by http:uri note:Tomcat only support GBK specification,so not set charset gb2312 --> <Connector URIEncoding="GBK" port="80" redirectPort="8443" maxSpareThreads="75" maxThreads="150" minSpareThreads="25"> </Connector>
Finally, in order to solve the problem of jsp garbled code, we also need to do the following, that is, add the following command to the left and right jsp headers:
<% @ Page contentType = "text/html; charset = gb2312" language = "java" %> or <% @ page pageEncoding = "gb2312" %>
3. JDBC and database
You can easily solve the garbled characters in writing and reading database data by using the following methods:
The JAVA program is processed in the method we specify.
Change the encoding format supported by the database to GBK or GB2312 by default.
At this point, Chinese problems can be solved for WEB applications. Of course, the above method is based on the unified encoding principle and the file conversion relationship (file-> class-> load-> execute or transfered or response or request) in the WEB mode.