all gone

          all gone

          JSP 處理MySQL數據庫時的中文問題

          以前遇到JSP 處理MySQL數據庫時的中文問題時,采取的是很笨的一種方法,直接用字符串編碼轉換函數進行轉換,這次從網上搜了一下,找到了一個使用Filter的可行方法。在Tomcat 5.5+ MySQL4.0.16下通過。

          filter類源碼是從網上找的,如下
          /**
           *
           */
          package com.lzy;

          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;

          /**
           * @author lzy
           *
           */
          public class SetCharacterEncodingFilter implements Filter {
           
           protected String encoding = null;
              protected FilterConfig filterConfig = null;
              protected boolean ignore = true;


           /* (non-Javadoc)
            * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
            */
           public void init(FilterConfig filterConfig) throws ServletException {
            // TODO Auto-generated method stub
            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;


           }

           /* (non-Javadoc)
            * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
            */
           public void doFilter(ServletRequest request, ServletResponse response,
             FilterChain chain) throws IOException, ServletException {
            
            // TODO Auto-generated method stub
            if (ignore || (request.getCharacterEncoding() == null)) {
             String encoding = selectEncoding(request);
             if (encoding != null)
              request.setCharacterEncoding(encoding);
            }
            chain.doFilter(request, response);
           }

           /* (non-Javadoc)
            * @see javax.servlet.Filter#destroy()
            */
           public void destroy() {
            // TODO Auto-generated method stub
            
            this.encoding = null;
               this.filterConfig = null;


           }
           
            protected String selectEncoding(ServletRequest request) {

                   return (this.encoding);

               }


          }

          在web.xml 文件中作如下設置:(我使用的是Struts框架)
          <filter>
          <filter-name>Encoding</filter-name>
          <filter-class>com.lzy.SetCharacterEncodingFilter</filter-class>
          <init-param>
          <param-name>encoding</param-name>
          <param-value>GBK</param-value>
          </init-param>
          </filter>

          <filter-mapping>
          <filter-name>Encoding</filter-name>
          <servlet-name>action</servlet-name>
          </filter-mapping>
          <filter-mapping>
          <filter-name>Encoding</filter-name>
          <url-pattern>*.jsp</url-pattern>
          </filter-mapping>


          最后,連接數據庫時,使用下面的url:
          jdbc:mysql://localhost:3306/數據庫名?useUnicode=true&characterEncoding=GBK

          posted on 2005-12-18 22:57 all gone 閱讀(481) 評論(0)  編輯  收藏 所屬分類: Java

          主站蜘蛛池模板: 五大连池市| 十堰市| 梨树县| 连江县| 凌源市| 沁阳市| 南木林县| 亚东县| 察哈| 拉孜县| 炉霍县| 靖安县| 瑞昌市| 滦平县| 定日县| 安福县| 芜湖县| 正蓝旗| 巴楚县| 抚松县| 兴仁县| 河东区| 安岳县| 昭苏县| 安仁县| 民丰县| 锡林郭勒盟| 甘孜县| 富平县| 海淀区| 密云县| 榆树市| 扬中市| 萝北县| 益阳市| 金湖县| 凤阳县| 温泉县| 兴文县| 磴口县| 定安县|