工大后院

 找回密码
 加入后院

扫一扫,访问微社区

QQ登录

只需一步,快速开始

搜索
查看: 1907|回复: 5

[求救]jsp连接mysql的中文问题

[复制链接]
发表于 2006-5-21 16:23 | 显示全部楼层 |阅读模式
对于页面编码,我这样设置
<%@ page contentType="text/html;charset=UTF-8" %>

整个页面不出现一个中文,都放在Resource_zh_CN.properties,并经过native2ascii转换.
这样调用<fmt:message key="page.login.title" />

这一切都可以正常.可是连接到mysql中,我这样
<value>jdbc:mysql://localhost:3306/course?useUnicode=true&amp;characterEncoding=UTF-8</value>

也试过characterEncoding=GB2312或GBK,可是读出来的数据总要经过这样才能正常显示
new String(buffer.getBytes("ISO-8859-1"),"GB2312");

后来我还试过改MYSQL的配置
原来是这样的
[mysql]
default-character-set=latin1
我改成
default-character-set=gbk (也试过utf8)
可是这样改后,我连在命令中向表里插入带中文的记录都不行,然后试试在网页表单插入,结果是乱码.

我好郁闷啊!!!!!网上关于这方面的资料很多,可又不尽相同,我试过几种都不成功.
哪位有成功经验的,给个方案吧!
先谢谢啦!
发表于 2006-5-21 16:54 | 显示全部楼层
1.mysql的字符集设置为gb2312

2.连接mysql时使用如下语句:"jdbc:mysql://yourhost:3306/yourdatabase?useUnicode=true&characterEncoding=GBK"

3.在页面使用:<%@ page contentType="text/html;charset=gb2312"%>

4.添加一个filter进行编码过滤。

  (1)。写个filter类

  1. public class SetCharacterEncodingFilter implements Filter {

  2.         protected String encoding = null;
  3.    
  4.     protected FilterConfig filterConfig = null;

  5.     protected boolean ignore = true;
  6.    

  7.         public void init(FilterConfig filterConfig) throws ServletException {
  8.                 // TODO 自动生成方法存根
  9.                 //使用配置的方法从web.xml中读入编码信息,避免硬编码。
  10.         this.filterConfig = filterConfig;
  11.         this.encoding = filterConfig.getInitParameter("encoding");
  12.         String value = filterConfig.getInitParameter("ignore");
  13.         if (value == null) {
  14.           this.ignore = true;
  15.         }
  16.         else if (value.equalsIgnoreCase("true")) {
  17.           this.ignore = true;
  18.         }
  19.         else if (value.equalsIgnoreCase("yes")) {
  20.           this.ignore = true;
  21.         }
  22.         else {
  23.           this.ignore = false;
  24.         }

  25.         }

  26.         public void doFilter(ServletRequest request, ServletResponse response,
  27.                         FilterChain chain) throws IOException, ServletException {
  28.                 // TODO 自动生成方法存根
  29.                 if (ignore || (request.getCharacterEncoding() == null)) {
  30.             String encoding = selectEncoding(request);
  31.             if (encoding != null) {
  32.             //正是通过这句代码实现的。
  33.                     request.setCharacterEncoding(encoding);
  34.             }
  35.     }   
  36.     // Pass control on to the next filter
  37.     chain.doFilter(request, response);
  38.         }

  39.     protected String selectEncoding(ServletRequest request) {
  40.         
  41.         return (this.encoding);

  42.       }

  43.    
  44.         public void destroy() {
  45.                 // TODO 自动生成方法存根
  46.                 this.encoding = null;
  47.         this.filterConfig = null;
  48.         }

  49.         /**
  50.          * @param args
  51.          */
  52.         public static void main(String[] args) {
  53.                 // TODO 自动生成方法存根

  54.         }

  55. }
复制代码


  (2)。在WEB-INF文件夹下的web.xml在添加:
  1.         <filter>
  2.                 <filter-name>Set Character Encoding</filter-name>
  3.                 <filter-class>SetCharacterEncodingFilter</filter-class>
  4.                 <init-param>
  5.                         <param-name>encoding</param-name>
  6.                         <param-value>gb2312</param-value>
  7.                 </init-param>
  8.         </filter>
  9.        
  10.           <filter-mapping>
  11.                 <filter-name>Set Character Encoding</filter-name>
  12.                 <url-pattern>/*</url-pattern>
  13.           </filter-mapping>
复制代码

[ 本帖最后由 hjack 于 2006-5-21 16:56 编辑 ]
回复

使用道具 举报

发表于 2006-5-21 17:29 | 显示全部楼层
我的做法是全部改成UTF-8,对于post获取的数据只用过滤器即可(hjack贴的代码),对于get的数据需要重编码,具体方法楼主的那个new String ×××那个就是。
回复

使用道具 举报

 楼主| 发表于 2006-5-22 11:10 | 显示全部楼层
谢谢两位!
虽然我的问题没有得到完全解决,但我好像知道问题所在.
我记得<<JSP2.0技术手册>>有个例子是讲解决MYSQL和JSP中文问题的,而且那时照着做是可以的,只是当时没在意.现在我去重新运行那个例子,竟然不行!我想应该是我的MYSQL或TOMCAT某些地方设置出了问题,暂时不想花太多时间去搞这个问题.我想我会慢慢发现的.
没办法,JAVA的入门就是这样,开始是CLASSPATH的问题,然后JSP就来个中文问题!呵呵~
回复

使用道具 举报

发表于 2006-5-22 13:05 | 显示全部楼层
楼上正解。我当时面对这个问题跟你的做法一样。
回复

使用道具 举报

发表于 2006-6-7 21:03 | 显示全部楼层
建议LZ去看下unicode方面的资料,明白下各种编码转化的最底层实现
迟点我发篇我个人总结的java编程注意的中文问题。包括了参数传递啊,读取数据库,。。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 加入后院

本版积分规则

QQ|Archiver|手机版|小黑屋|广告业务Q|工大后院 ( 粤ICP备10013660号 )

GMT+8, 2025-5-15 00:42

Powered by Discuz! X3.5

Copyright © 2001-2024 Tencent Cloud.

快速回复 返回顶部 返回列表