从jsp页面到数据库里中文乱码问题

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

<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>  

<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>  

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

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

<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/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值