解决存到数据库里中文乱码问题

1.页面

  • 每个页面上加上  这样在jsp页面里,点右键,查看编码方式则为UTF-8.

[java]  view plain copy
  1. <%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>  

[java]  view plain copy
  1. <%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>  

  • 也可以把设置myEclipse中默认的contentTyepe。步骤是:windows->preferences->Generl->ContentType.然后设置成UTF-8.一定要update~~
  • 在JSP页面头部加入下面这句话,告诉浏览器应该调用UTF-8的字符集。

  1. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>  

[java]  view plain copy
  1. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>  

 

2.数据库连接语句

  •  设置characterencoding为UTF-8 如jdbc.mysql.url=jdbc:mysql://localhost:3306/db?useUnicode=true&amp;characterEncoding=UTF8
  • 如果使用Hibernate,那就把所有的配置文件头部的编码格式改成UTF-8。

3.Tomcat

  •    为了保证get/post数据都采用相同的UTF8编码,我们在server.xml中进行了如下设置: <Connector port="8080" protocol="HTTP/1.1"                connectionTimeout="20000"                redirectPort="8443"  URIEncoding="UTF-8" />

4. struts2配置

  • struts2在配置struts.properties文件中加上struts.i18n.encoding=UTF-8 或者在struts.xml里加上<constant name="struts.i18n.encoding" value="UTF-8" />

在web.xml文件里配置filter 

<filter>
  <filter-name>encodingFilter</filter-name>
  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  <init-param>
   <param-name>encoding</param-name>
   <param-value>GBK</param-value>
  </init-param>
 </filter>
 
 <filter-mapping>
  <filter-name>encodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping> 

5.数据库(mysql)

可以先输入查询语句SHOW VARIABLES LIKE 'character_set_%';,查看所有的编码是否是UTF-8.

如果不是可以使用Server Instance Config 把默认的字符集设置为utf-8或者修改/MySQL/MySQL Server 5.0/my.ini中的default-character-set=gbk

character-set-server=gbk;

然后重新启动mysql的服务就行了

原帖:http://blog.csdn.net/chenmeng2192089/article/details/7994352

猜你喜欢

转载自blog.csdn.net/fszeng2011/article/details/45256715