mysql+tomcat乱码全攻略!
一:mysql引起的乱码解决方案 
一般由以下几种原因引起: 
(1)java中处理中文字符正常,在cmd client中显示乱码是字符集的问题. 
(2)字段长度设置够长,但插入中文字符时提示   com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column错误是字符集的问题. 
(3)乱码问题归根到底是字符集的问题,那就从字符集设置方面考虑,不外乎下面几个方面:server,client,database,connection,results. 
-------------------------解决办法---------------------- 
(1)修改 my.ini(MySQL Server Instance Configuration 文件) 
# CLIENT SECTION 
[client] 
port=3306 
[mysql] 
default-character-set=utf-8 
# SERVER SECTION 
[mysqld] 
default-character-set=utf-8 
(2)修改data目录中相应数据库目录下的db.opt配置文件 
default-character-set=utf-8 
default-collation=utf8_general_ci 
(3)数据库连接串中指定字符集 
URL=jdbc:mysql://yourIP/college?user=root&password=yourPassword&useUnicode=true&characterEncoding=utf-8 
(4)在创建数据库时指定字符集 
create database yourDB CHARACTER SET utf-8; 
ok,经过以上4个方面的设置,基本上mysql引起的乱码问题已经解决! 
二:tomcat下的乱码解决方案 
  首先将乱码问题分为三类JSP页面显示中文乱码;表单提交乱码;数据库应用乱码 
1,JSP页面内输出中文时出现乱码 
   解决方案在JSP文件中使用page命令指定响应结果的MIME类型,如<%@ page   language="java" contentType="text/html;charset=utf-8" %> 
2,表单提交乱码(分为post和get两种提交方式) 
  使用request.getParameter方法得到乱码,这是因为tomcat处理提交的参数时默认的是iso-8859-1,表单提交get和post处理乱码问题不同,下面分别说明。 
(1)POST处理方式 
   对post提交的表单通过编写一个过滤器的方法来解决,过滤器在用户提交的数据被处理之前被调用,可以在这里改变参数的编码方式, 
过滤器的代码如下: 
package com.webim.search; 
import javax.servlet.*; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
public class FileFilter implements Filter { 
protected String encoding = null; 
protected FilterConfig filterConfig = null; 
protected boolean ignore = true; 
public void init(FilterConfig filterConfig) 
throws javax.servlet.ServletException { 
this.filterConfig = filterConfig; 
this.encoding = filterConfig.getInitParameter("encoding"); 
String value = filterConfig.getInitParameter("ignore"); 
if (value == null) { 
this.ignore = true; 
} else if (value.equalsIgnoreCase("true")) { 
this.ignore = true; 
} else if (value.equalsIgnoreCase("yes")) { 
this.ignore = true; 
} else { 
this.ignore = false; 
} 
} 
public void doFilter(ServletRequest request, ServletResponse response, 
FilterChain chain) throws java.io.IOException, 
javax.servlet.ServletException { 
if (ignore || (request.getCharacterEncoding() == null)) { 
String encoding = selectEncoding(request); 
if (encoding != null) { 
request.setCharacterEncoding(encoding); 
} 
} 
chain.doFilter(request, response); 
} 
public void destroy() { 
this.encoding = null; 
this.filterConfig = null; 
} 
protected String selectEncoding(ServletRequest request) { 
return (this.encoding); 
} 
} 
web.xml文件加入过滤器: 
<filter> 
<filter-name>FileFilter</filter-name> 
<filter-class>com.webim.search.FileFilter</filter-class> 
<init-param> 
<param-name>encoding</param-name> 
<param-value>utf-8</param-value> 
<!--gbk或者gb2312或者utf-8--> 
</init-param> 
<init-param> 
<param-name>ignore</param-name> 
<param-value>true</param-value> 
</init-param> 
</filter> 
<filter-mapping> 
  <filter-name>FileFilter</filter-name> 
  <servlet-name>/*</servlet-name> 
</filter-mapping> 
* 注意filter元素要放在所有web.xml元素之前。 
(2) Get方法的处理 
tomcat对post和get的处理方法不一样,所以过滤器不能解决get的乱码问题,它需要在其他地方设置。 
打开<tomcat_home>\conf目录下server.xml文件,找到对8080端口进行服务的Connector组件的设置部分,给这个组件添加一个属性:URIEncoding="utf-8"。修改后的Connector设置为: 
  <Connector port="8080" maxHttpHeaderSize="8192" 
               maxThreads="150" minSpareThreads="25" maxSpareThreads="75" 
               enableLookups="false" redirectPort="8443" acceptCount="100" 
               connectionTimeout="20000" useBodyEncodingForURI="true"     URIEncoding="utf-8" disableUploadTimeout="true" /> 
  * 注意修改后重新启动tomcat才能起作用。 
上面这一点修改很重要,我在工作中就遇到过这样的乱码问题,配置好后就解决了!很实用! 
(3)数据库的乱码在前面已经介绍! 
                    
                
                
            
        
浙公网安备 33010602011771号