J2ee|web| Solutions | questions | Chinese
1, HTML
Whether it's standalone HTML or other programs, such as servlet, note that meta tags must be added to and from the final HTML to specify the encoding of the input characters in HTML, such as:
2. JSP and Servlet
You must first resolve 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 filtering function, The Jsp/servlet container or server-provided feature settings that you need to use, as in Tomcat5.5.9, can be set in Webapps/yourappdirectory/web-inf/web.xml 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>
|
Where Setcharsetencodingfilter class is the filter class that is used to set the request and reponse character encodings, where the setting statement is as follows:
Request.setcharacterencoding (targetencoding); Response.setcontenttype ("text/html"); Response.setcharacterencoding (targetencoding);
|
In addition, in order to solve the garbled problem of passing parameters through get (with parameters in the URL), we also need to set up the encoding required for the URL pass parameter, which can be Tomcat5.5.9 in the ${tomcat_home}\conf\server.xml AndSet up between, as follows:
<!--uriencoding= "GBK": Force Get Method String (Chinese) can is transferd properly by Http:uri Note:tomcat Support GBK Specification,so not set charset gb2312--> <connector uriencoding= "GBK" port= "" redirectport= "8 443 "maxsparethreads=" maxthreads= " minsparethreads=" > </Connector>
|
Finally, in order to solve the JSP garbled problem, we also need to do the following processing, that is, in the left and right JSP head plus the following instructions:
<%@ page contenttype= "text/html;charset=gb2312" language= "java"%> or <%@ page pageencoding= "gb2312"%>
|
3. JDBC and database
The problem of garbled writing to the database and reading the database data can be easily resolved by the following ways:
The processing methods for Java programs are handled in the way we specify.
Change the encoding format supported by the database default to GBK or GB2312.
In this way, the Chinese problem can be solved in general for Web application. Of course, the above methods are based on the principle of unified coding and the Web-mode file conversion relationship (File->class->load->execute or transfered or response or request).