qiyadeng

          專注于Java示例及教程
          posts - 84, comments - 152, trackbacks - 0, articles - 34

          導航

          公告

          更多請訪問,www.qiyadeng.com
          <2005年10月>
          2526272829301
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          常用鏈接

          留言簿(12)

          文章分類(35)

          相冊

          J2EE

          Others

          Tools

          積分與排名

          • 積分 - 310904
          • 排名 - 184

          最新評論

          閱讀排行榜

          中文,又是中文!

          Posted on 2005-10-20 16:12 qiyadeng 閱讀(620) 評論(0)  編輯  收藏

          該死的!不得不讓我說臟話了!莫名其妙的問題。
          現(xiàn)在算是比較清楚了。最近正在做一個分布式的系統(tǒng)的整合。中心數(shù)據(jù)庫用的utf8的編碼,以前的老的系統(tǒng)用的是latin1的編碼。
          在latin1的編碼中插入和查詢數(shù)據(jù):
          不用在連接字符上花功夫。
          只要下面這個類,把中文轉(zhuǎn)換為latin1的編碼,因為默認的連接是lanti1的所以還要另外的連接字符串嗎?

           1/*
           2 * Created on 2005-8-15
           3 *
           4 * TODO To change the template for this generated file go to
           5 * Window - Preferences - Java - Code Style - Code Templates
           6 */

           7package com.motel168.util;
           8
           9/**
          10 * @author qiya
          11 * 
          12 * TODO To change the template for this generated type comment go to Window -
          13 * Preferences - Java - Code Style - Code Templates
          14 */

          15public class Chinese {
          16    public static String toChinese(String iso)
          17        String gb; 
          18        try
          19            if(iso.equals(""|| iso == null)
          20                return ""
          21            }
           
          22            else
          23                iso = iso.trim(); 
          24                gb = new String(iso.getBytes("ISO-8859-1"),"GB2312"); 
          25                return gb; 
          26            }
           
          27        }
          catch(Exception e)
          28            System.err.print("編碼轉(zhuǎn)換錯誤:"+e.getMessage()); 
          29            return ""
          30        }

          31    }

          32    public static String toLatin(String iso)
          33        String gb; 
          34        try
          35            if(iso.equals(""|| iso == null)
          36                return ""
          37            }
           
          38            else
          39                iso = iso.trim(); 
          40                gb = new String(iso.getBytes("GB2312"),"ISO-8859-1"); 
          41                return gb; 
          42            }
           
          43        }
          catch(Exception e)
          44            System.err.print("編碼轉(zhuǎn)換錯誤:"+e.getMessage()); 
          45            return ""
          46        }

          47    }

          48
          49}

          50

          在utf8編碼的那一段更簡單,所有的編碼設為utf8。
          上次mysql中文問題提到過,就不再提了。
          另外使用hibernate的時候,也會出現(xiàn)一些中文問題,這時候需要進行如下設置:
          在hibernate.cfg.xml的配置文件中加入:
          <property name="connection.characterEncoding">UTF-8</property>
          同樣不需要在連接字符串上加入?yún)?shù)。
          然后使用Filter:
          在web.xml中加入如下信息:
              <filter>
                  <filter-name>filter-name</filter-name>
                  <filter-class>com.motel168.util.SetEncodeFilter</filter-class>
                  <init-param>
                      <param-name>defaultencoding</param-name>
                      <param-value>UTF-8</param-value>
                  </init-param>
              </filter>
              <filter-mapping>
                  <filter-name>filter-name</filter-name>
                  <url-pattern>/*</url-pattern>
              </filter-mapping>
          對應的類為:
          package com.motel168.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 SetEncodeFilter implements Filter {

              protected FilterConfig filterConfig = null;

              protected String defaultEncoding = null;

              public void init(FilterConfig arg0) throws ServletException {
                  this.filterConfig = arg0;
                  this.defaultEncoding = filterConfig.getInitParameter("defaultencoding");

              }

              public void doFilter(ServletRequest request, ServletResponse response,
                      FilterChain chain) throws IOException, ServletException {

                  request.setCharacterEncoding(selectEncoding(request));
                  chain.doFilter(request, response);

              }

              public void destroy() {
                  this.defaultEncoding = null;
                  this.filterConfig = null;

              }

              protected String selectEncoding(ServletRequest request) {
                  return this.defaultEncoding;
              }

          }



          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 邢台市| 四会市| 临邑县| 柯坪县| 堆龙德庆县| 搜索| 正阳县| 奈曼旗| 宁都县| 海丰县| 永顺县| 韶山市| 宜都市| 德昌县| 武宣县| 柞水县| 独山县| 平远县| 沂源县| 农安县| 绩溪县| 吐鲁番市| 措美县| 铜梁县| 济阳县| 赞皇县| 柘城县| 南皮县| 常德市| 北辰区| 六安市| 崇信县| 杭州市| 灌阳县| 崇州市| 岚皋县| 抚宁县| 深泽县| 大埔区| 曲松县| 区。|