When deploying a webservices program today, the servlet that obtains data from the page encountered a garbled problem. In the servlet, I added the text request.setCharacterEncoding("GB2312"); to the code, but the garbled problem still occurred.
Find information online, as follows: JAVA is Unicode encoding, you first convert it to ISO8859-1, and then convert it to GBK or GB2312.
java code
This is Chinese!
There is an easier way, which is to set the encoding conversion directly in the server. TOMCAT5.0.28 is used.
Find the SERVER.XML file in the ../Tomcat 5.0/conf directory, open it with WordPad, and set it as follows:
Just add an attribute inside, URIEncoding="GBK"
The test is passed and it is done. Then there is what role the sentence request.setCharacterEncoding("GB2312") plays. If it cannot solve the messy problem, what role do we use it to play? The information is organized as follows:
(1) The JSP page is in Chinese, but when you look at it, it is garbled:
The solution is to encode the JSP page. Because of the encoding problem when JSP is converted into a Java file, some servers use ISO-8859-1 by default. If Chinese is directly entered into a JSP, JSP treats it as ISO8859. -1 is definitely a problem. We can confirm this by looking at the Java intermediate file generated by Jasper.
(2) When using the Request object to obtain the Chinese character code submitted by the customer, garbled characters will appear:
The solution is to configure a filter, which is a Servelet filter. The code is as follows:
request.setCharacterEncoding("GBK");
// Pass control to the next filter
chain.doFilter(request, response);
}
<filter-mapping></filter-mapping>
<filter-name></filter-name>Set Character Encoding
<url-pattern></url-pattern>/*
There is also the processing of information containing Chinese characters. The processing code is:
The solution is:
1) Open the server.xml file of tomcat, find the block, and add the following line:
URIEncoding=”GBK”
The complete should be as follows:
(4) There are Chinese characters on the JSP page and Chinese characters on the buttons, but when viewing the page through the server, garbled characters appear:
The solution is: First, the localized message text should not be included directly in the JSP file, but the text should be obtained from the Resource Bundle through the <bean:message> tag. You should put your Chinese text in the Application.properties file. This file is placed under WEB-INF/classes/*. For example, if I have two labels for name and age on the page, I first need to create an Application.properties. The content inside should be name="name" age="age", then I put this file under WEB-INF/classes/properties/, and then encode it according to the Application.properties file to create a Chinese Resource file, assuming the name is Application_cn.properties. The native2ascii command is provided in the JDK, which can realize character encoding conversion. Find the directory where you placed the Application.properties file in the DOS environment. Execute the command in the DOS environment. The Chinese resource file Application_cn.properties encoded in GBK will be generated: native2ascii ?encoding gbk Application.properties Application_cn.properties. Execute the above command. In the future, the Application_cn.properties file with the following content will be generated: name=u59d3u540d age=u5e74u9f84, configured in Struts-config.xml: <message-resources parameter="properties.Application_cn"></message-resources>. At this point, more than half of it is basically done. Then you have to write on the JSP page. For the label of the name, you need to write <bean:message key=""name"">. This will appear on the page. The Chinese name will appear, the same goes for the age, and the Chinese characters on the button are also processed in the same way.
(5) The code written to the database is garbled:
Solution: Configure a filter, which is a Servelet filter. The code is the same as the second time.
If you are directly connected to the database through JDBC, the configuration code is as follows: jdbc:mysql://localhost:3306/workshopdb? useUnicode=true&characterEncoding=GBK, so as to ensure that the code in the database is not garbled.
If you are linking through the data source, you cannot write it like this. First, you have to write it in the configuration file. The place to configure the data source in tomcat 5.0.19 is under C:Tomcat 5.0confCatalinalocalhost. I created The project is workshop, and the directory where it is placed is under webapp. The configuration file of workshop.xml is as follows:
<resource name="jdbc/WorkshopDB"></resource>auth="Container"
type="javax.sql.DataSource" />
<resourceparams name="jdbc/WorkshopDB"></resourceparams>
<parameter></parameter>
<name></name>factory
<value></value>org.apache.commons.dbcp.BasicDataSourceFactory
<parameter></parameter>
<name></name>maxActive
<value></value>100
<parameter></parameter>
<name></name>maxIdle
<value></value>30
<parameter></parameter>
<name></name>maxWait
<value></value>10000
<parameter></parameter>
<name></name>username
<value></value>root
<parameter></parameter>
<name></name>password
<value></value>
<parameter></parameter>
<name></name>driverClassName
<value></value>com.mysql.jdbc.Driver
<parameter></parameter>
<name></name>url
<value></value>
Pay special attention to the places in bold. It is different from the direct link to JDBC. If you configure it correctly, when you enter Chinese, it will be Chinese in the database. One thing to note is that when you display The data page also needs this line of code. It should be noted that some front desk staff use Dreamver to write the code. When writing a Form, they change it to a jsp. There is one thing to pay attention to, and that is the submission of Action in Dreamver. The method is request, and you need to send it over, because there are two methods of JSP submission: POST and GET, but the code submitted by these two methods is still very different in terms of encoding. This is Explained later. </bean:message>< /bean:message>