Mysql与JSP网页中文出现乱码问题的解决方案
发布时间:2023-06-03 13:26:29 所属栏目:教程 来源:
导读:自从以前学习jsp开始,中文乱码问题就一直不断,苦不堪言。这次在项目开始之前,我们要解决的第一个问题就是把mysql的中文乱码问题搞定。经过多天的努力,终于成功的解决了中文乱码问题,特写在这里,以备后用。
自从以前学习jsp开始,中文乱码问题就一直不断,苦不堪言。这次在项目开始之前,我们要解决的第一个问题就是把mysql的中文乱码问题搞定。经过多天的努力,终于成功的解决了中文乱码问题,特写在这里,以备后用。 软件及环境:windows xp(2000), j2sdk1.4.2, tomcat 5.0.25, mysql 4.1, ems mysql manager 2(方便建表,版本2.8.5.1),驱动为mysql-connector-java-3.1.4-beta-bin.jar。 目标:在该环境下,实现中文的正常显示,读取与插入数据库。 注:我只在此环境下测试通过,别的系统及不同版本未测试 要点:统一字符集(jsp页面编码,mysql建库时字符集选择,连接数据库url,request设定等) 下面我以gbk为例讲解。如果要使用utf-8,只要在相应的gbk处换成utf-8即可 --------------------------- 步骤1 以gbk字符集建库建表 ------------------------------------- 我使用ems来建mysql的数据库及表,因为它是图形界面,方便操作(就像sql server 2000中的企业管理器一样)。 建库时,从ems菜单中选create database...新建一个数据库,characterset选gbk_bin(另一个gbk_chinese_ci不知道与这个有什么区别,我找资料也没有找到。如果你知道,请告诉我,我补充在这里)。不要把工具栏上有一个加号和数据库模样的图标当成新建数据库了,那个新注册一个已经存在的数据库。 后面建表时,也要选择同样的字符集。 建好后,此时不要用ems向里面插入数据,否则你看到的中文依然是乱码。 --------------------------- 步骤2 连接数据库的url后加些参数 ------------------------------- 假设我新建的数据库是testdb,那么我连接数据库的url应该为: jdbc:mysql://localhost:3306/testdb?useunicode=true&characterencoding=gbk 此时要注意:如果我是把这个url写在java代码中,就直接这样写。但如果是在xml配置文件中(如struts-config.xml,web.xml等),要把其中的&改为&才行,否则会出错。也就是: jdbc:mysql://localhost:3306/testdb?useunicode=true&characterencoding=gbk --------------------------- 步骤3 每个jsp页面都要声明该中文字符集 ---------------------------- 在每个jsp页面的最上面都加上一句 <%@ page language="java" contenttype="text/html;charset=gbk" %> 这样才能保证jsp页面中的中文显示正常 --------------------------- 步骤4 加一个传递参数时设定request字符集的filter类 ----------------------- 因为网络中字符在传递的时候,都是统一以iso-8859-1的编码传递,所以我们必须对request重新设定字符集,才能正常显示中文。如果采用filter类来实现,我们不用在每次取中文参数时都要重新设定。 filter类的内容: /* * ==================================================================== * * javawebstudio 开源项目 * * struts_db v0.1 * * ==================================================================== */ package com.strutslogin.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 setcharacterencodingfilter implements filter { // ----------------------------------------------------- instance variables /** * the default character encoding to set for requests that pass through * this filter. */ protected string encoding = null; /** * the filter configuration object we are associated with. if this value * is null, this filter instance is not currently configured. */ protected filterconfig filterconfig = null; /** * should a character encoding specified by the client be ignored? */ protected boolean ignore = true; // --------------------------------------------------------- public methods /** * take this filter out of service. */ public void destroy() { this.encoding = null; this.filterconfig = null; } /** * select and set (if specified) the character encoding to be used to * interpret request parameters for this request. * * @param request the servlet request we are processing * @param result the servlet response we are creating * @param chain the filter chain we are processing * * @exception ioexception if an input/output error occurs * @exception servletexception if a servlet error occurs */ public void dofilter(servletrequest request, servletresponse response, filterchain chain) throws ioexception, servletexception { // conditionally select and set the character encoding to be used if (ignore || (request.getcharacterencoding() == null)) { string encoding = selectencoding(request); if (encoding != null) request.setcharacterencoding(encoding); } // pass control on to the next filter chain.dofilter(request, response); } /** * place this filter into service. * * @param filterconfig the filter configuration object */ public void init(filterconfig filterconfig) throws 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; } // ------------------------------------------------------ protected methods /** * select an appropriate character encoding to be used, based on the * characteristics of the current request and/or filter initialization * parameters. if no character encoding should be set, return * <code>null</code>. * <p> * the default implementation unconditionally returns the value configured * by the <strong>encoding</strong> initialization parameter for this * filter. * * @param request the servlet request we are processing */ protected string selectencoding(servletrequest request) { return (this.encoding); } }//eoc 该代码来自于www.javawebstudio.com,特此感谢! 然后我们在web.xml中加一些配置,就可以了,配置如下: <filter> <filter-name>set character encoding</filter-name> <filter-class>javawebstudio.struts_db.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> </init-param> </filter> <filter-mapping> <filter-name>set character encoding</filter-name> <servlet-name>action</servlet-name> </filter-mapping> 放在web.xml的合适位置。一般在最后,<jsp-config>标签之前(如果有的话) 经过以上步骤,jsp和mysql的中文显示及插入就都正常了。在struts中也正常。 但是,此时如果你用ems或mysql的命令行控制台来看表中的数据,却发现它们都是????。这是怎么回事呢? 不用担心,只要我们运行下面的这几行命令,就能看到正常的中文了! set character_set_client = gbk; set character_set_connection = gbk; set character_set_database = gbk; set character_set_results = gbk; set character_set_server = gbk; set collation_connection = gbk_bin; set collation_database = gbk_bin; set collation_server = gbk_bin; 如果你用的是mysql的命令行,则直接输入就好。 如果是ems,则在工具栏中有一个show sql editor按钮,点一下,把上面的命令输入,再按一个"execute"的按钮,就行了! 而且在这种情况下,你可以甚至可以用中文名来建数据库,表名和字段名!!!! 但是有一点要特别注意! 像gbk,utf-8这样的名字,在mysql与java中有不同的规定,写的时候要格外注意,否则会出错。 比如gbk,在java中要写成gbk,但在mysql中要写成gbk(连接数据库的url) 比如utf-8,在java中要写成utf-8,但在mysql中要写成utf8 其它的字集符也有类似的区别 (编辑:聊城站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
站长推荐