成長中的記憶
          這是我的成長,這是我的天地,學習JAVA,只因快樂。
          posts - 4,comments - 16,trackbacks - 0
          web.xml
          <filter>
          ????????
          <filter-name>Set?Character?Encoding</filter-name>
          ????????
          <filter-class>mypack.Charset</filter-class>
          ????????
          <init-param>
          ????????????
          <param-name>encoding</param-name>
          ????????????
          <param-value>UTF-8</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>
          過濾的類
          package mypack;
          import?java.io.*;
          import?javax.servlet.*;

          public?class?Charset?implements?Filter?{
          ????
          protected?String?encoding?=?null;
          ????
          protected?FilterConfig?filterConfig?=?null;
          ????
          protected?boolean?ignore?=?true;

          ????
          public?void?destroy()?{
          ????????
          this.encoding?=?null;
          ????????
          this.filterConfig?=?null;
          ????}


          ????
          public?void?doFilter(ServletRequest?request,?ServletResponse?response,
          ????????????FilterChain?chain)
          throws?IOException,?ServletException?{
          ????????
          if?(ignore?||?(request.getCharacterEncoding()?==?null))?{
          ????????????String?encoding?
          =?selectEncoding(request);
          ????????????
          if?(encoding?!=?null)
          ????????????????request.setCharacterEncoding(encoding);
          ????????}

          ????????chain.doFilter(request,?response);
          ????}


          ????
          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?String?selectEncoding(ServletRequest?request)?{
          ????????
          return?(this.encoding);
          ????}

          }

          這2個加進工程里,就可以直接運行了。不用在設置什么。
          至于數據庫最好設置為UTF-8.
          從此以后遠離中文出現亂碼的煩惱。哈哈哈
          posted on 2006-03-01 11:16 aiyoyoyo 閱讀(4017) 評論(2)  編輯  收藏 所屬分類: Struts

          FeedBack:
          # re: Struts過濾器的使用,已解決中文問題
          2008-12-30 01:01 | billmao
          好,直接房項目里改一下web.xml里的路徑就可以用了  回復  更多評論
            
          # re: Struts過濾器的使用,已解決中文問題[未登錄]
          2009-12-11 10:06 | qin
          這不是struts過濾器  回復  更多評論
            

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


          網站導航:
           
          主站蜘蛛池模板: 利川市| 枞阳县| 井陉县| 大足县| 九江县| 吉安县| 托克逊县| 新河县| 台北县| 清苑县| 双柏县| 金溪县| 双鸭山市| 安吉县| 邯郸市| 略阳县| 石狮市| 玉田县| 资兴市| 苍南县| 黄龙县| 高邮市| 贞丰县| 灵寿县| 金山区| 驻马店市| 疏勒县| 综艺| 洪洞县| 衢州市| 安达市| 黄浦区| 南陵县| 栖霞市| 无为县| 南汇区| 教育| 台山市| 长岛县| 土默特右旗| 双柏县|