成長中的記憶
          這是我的成長,這是我的天地,學習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 閱讀(4022) 評論(2)  編輯  收藏 所屬分類: Struts

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

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


          網站導航:
           
          主站蜘蛛池模板: 罗城| 栖霞市| 南城县| 清流县| 扎赉特旗| 酒泉市| 彰武县| 明水县| 绿春县| 云南省| 肃宁县| 东宁县| 调兵山市| 湘潭市| 临沭县| 昆山市| 乌恰县| 江门市| 竹溪县| 深泽县| 云阳县| 云南省| 怀远县| 塔城市| 安义县| 墨玉县| 定南县| 长宁县| 教育| 利津县| 景洪市| 万全县| 上犹县| 龙川县| 吉隆县| 扶风县| 焉耆| 青田县| 高邮市| 和林格尔县| 英山县|