Solution to Chinese garbled characters in tomcat + SSH,

Source: Internet
Author: User

Solution to Chinese garbled characters in tomcat + SSH,

Recently, it has switched to J2EE development. It has been useless for a long time.


Local ubuntu 12.13, mysql5.x, tomcat7.x, struts2.3.15.x, spring3.1.0, hibernate4.1.x


I have been connecting to the server to test the database. No Chinese garbled characters have been found.

Now we can build this environment locally and use a local database to facilitate debugging. However, we find that every time we operate on the database, there will be Chinese characters in it ???? Garbled.


So what causes the problem step by step:

For the display of garbled characters in the page, this problem is relatively simple, is to check whether your JSP file is a Chinese to handle, because the default JSP encoding format is "ISO-8859-1 ", when JSP displays the Chinese characters to be processed, garbled characters are displayed. This usually occurs when JSP is handwritten or modified.
<%@ page language="java" contentType="text/html; charset=utf-8"pageEncoding="UTF-8"%>
2. garbled characters occur when parameters are transmitted. The parameter content is in Chinese. Whether it is post or get.
Note:
  • Tomcat configuration, Location: tomcat-> conf-> server. xml, add URIEncoding = "UTF-8"
 <Connector port="8080" protocol="HTTP/1.1"               connectionTimeout="20000"               redirectPort="8443"                URIEncoding="utf-8"/>

  • Add filter to web. xml
<filter>    <filter-name>encodingFilter</filter-name>    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>    <init-param>      <param-name>encoding</param-name>      <param-value>UTF-8</param-value>    </init-param>  </filter>  <filter-mapping>    <filter-name>encodingFilter</filter-name>    <url-pattern>/*</url-pattern>  </filter-mapping>

Web character filter with spring used in the above project
  • Tomcat configuration, Location: tomcat-> conf-> web. xml open the comment Filter
<!-- A filter that sets character encoding that is used to decode -->  <!-- parameters in a POST request -->    <filter>        <filter-name>setCharacterEncodingFilter</filter-name>        <filter-class>org.apache.catalina.filters.SetCharacterEncodingFilter</filter-class>        <init-param>            <param-name>encoding</param-name>            <param-value>UTF-8</param-value>        </init-param>        <async-supported>true</async-supported>    </filter>

  • Write Filter (this is useless)
3. The background mainly focuses on hibernate and mysql (mysql database used in my project)
  • Hibernate note:
Connect to mysql. If hibernate. cfg. xml is used with the attribute.
<Property name = "connection. useUnicode"> true </property>
<Property name = "connection. characterEncoding "> UTF-8 </property> if you use spring configuration file: There are two methods, 1, directly write to applicationContext. xml; 2, written to the database-config.properties, by applicationContext. xml call database-config.properties:
mydata.driverClass=com.mysql.jdbc.Driver#mydata.url=jdbc:mysql://localhost:3306/sdk?useUnicode=true&amp;characterEncoding=UTF-8mydata.url=jdbc:mysql://localhost:3306/sdk?useUnicode=true&characterEncoding=UTF-8mydata.username=rootwshouyouDB.password=xxxx
(The problem I encountered is that in the code above) applicationContext. xml:
<! -- Read jdbc. properties configuration file --> <beanclass = "org. springframework. beans. factory. config. propertyplaceholderpolicer "> <property name =" locations "> <value> classpath: database-config.properties </value> </property> </bean> <bean id = "dataSource" destroy-method = "close" class = "com. mchange. v2.c3p0. comboPooledDataSource "> <property name =" driverClass "value =" $ {mydata. driverClass} "/> <property name =" jdbcUrl "value =" $ {mydata. url} "/> <property name =" user "value =" $ {mydata. username} "/> <property name =" password "value =" $ {mydata. password} "/>

  • Mysql Section
When creating a database table, note: show create table gamepad_model; check the statements used to create a table.
CREATE TABLE `gamepad_model` (  `edit_id` varchar(50) NOT NULL,  `filedata` text,  `gamepad_type` varchar(100) NOT NULL,  `pic_url` varchar(255) DEFAULT NULL,  `sdk_version` varchar(50) NOT NULL,  `update_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,  PRIMARY KEY (`edit_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8
Need to add: CHARSET = utf8
To eliminate the problem of mysql, You can manually insert a piece of data containing Chinese characters to check whether the table contains garbled characters;
Insert into gamepad_model (edit_id, filedata, gamepad_type, sdk_version) values ('test3 _ test', 'tttt', 'testtt _ your', '1 ');
If no problem exists, mysql is excluded. 1. Check whether garbled characters exist in the foreground. No. Pass the parameter layer. 2. Pass the parameter to java to see the passed Chinese characters, whether Chinese characters are garbled in debug. If not, exclude 3. After the first two steps are excluded, it should be a background problem, hibernate or mysql problem. 4. You can check the above first, manually insert a statement with Chinese content to check whether there are garbled characters in the database. If not, exclude Step 5. At this step, only the hibernate problem is left. As mentioned above, check the note. My problem lies in this step,
mydata.url=jdbc:mysql://localhost:3306/sdk?useUnicode=true&characterEncoding=UTF-8
Generally, it is not in xml. to connect to the database, you must add
useUnicode=true&characterEncoding=UTF-8
However, if the xml file contains mydata. url = jdbc: mysql: // localhost: 3306/sdk? UseUnicode = true & amp; characterEncoding = other aspects of the UTF-8 have not been studied, if you have, please advise!

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.