为什么从mysql读出中文数据到页面显示是乱码解决方案

   阅读
为什么从mysql读出中文数据到页面显示是乱码
我是用struts+hibernate架构,在struts-config.xml中的数据源设成了<set-property property="url"
value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf-8" />
页面上也加了<%@ page contentType="text/html;charset=UTF-8" language="java"%>
页面中输上的中文没有问题,但从mysql中读取中文时就会出现乱码
我用的是mysql5.0
请帮忙解决,谢谢!



------解决方案--------------------
方案一:<%@ page contentType="text/html;charset=UTF-8" language="java"%> 加上属性pageEncoding="UTF-8"试试。
方案二:做个过滤器试。
不过首先确定数据库中的中文不是乱码
------解决方案--------------------
探讨
server处理客户请求
jsp页面编码
数据库编码
都统一使用同样的编码

------解决方案--------------------
乱码问题肯定是编码不一致.我一般是写过滤器.
MYSQL设置编码要从MY.INI里改
------解决方案--------------------
首先你的mysql数据库的编码设置必须为gb2312
读取数据显示乱码可以用response.setContentType()方法设置编码为Gb2312
------解决方案--------------------
楼主真正的把GBK 转成UTF8了吗 是那种真正的转编码,其实不建议用UTF-8 用GBK的话挺好的,如果你只对国内用户开放的话
------解决方案--------------------


package com.ufinity.mealsordering.common.util;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class EncodingFilter implements Filter {
private String _charSet = null;


public void destroy() {
}

public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
if (null == this._charSet) {


arg0.setCharacterEncoding("UTF-8");


} else {

arg0.setCharacterEncoding(this._charSet);
}
arg2.doFilter(arg0, arg1);
}


public void init(FilterConfig arg0) throws ServletException {
this._charSet = arg0.getInitParameter("charSet");
}

}
 
你那是数据库和页面的编码不同的问题,你加下上面的过滤器试试。
------解决方案--------------------
用过滤器就不用管页面和数据库的编码了吧
我刚加了
也配置了web.xml文件
<?xml version="1.0" encoding="GBK"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>
first</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<filter> 
<filter-name>Set Character Encoding</filter-name> 
<filter-class>filter.SetCharacterEncodingFilter</filter-class> 
<init-param> 
<param-name>encoding</param-name> 
<param-value>GBK</param-value> 
</init-param> 
<init-param> 
<param-name>ignore</param-name> 
<param-value>true</param-value> 
阅读
上一篇:js如何调用参数 下一篇:返回列表