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

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

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


          網站導航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           
          主站蜘蛛池模板: 济源市| 泌阳县| 石首市| 西畴县| 商城县| 贵南县| 衢州市| 临澧县| 贡山| 玉林市| 天峻县| 廉江市| 漳州市| 临海市| 武安市| 水城县| 辽宁省| 札达县| 莒南县| 方山县| 江油市| 固始县| 藁城市| 连城县| 弥勒县| 肃宁县| 东阳市| 古丈县| 玛曲县| 高邑县| 太仓市| 宿松县| 靖西县| 新丰县| 广南县| 潜江市| 上杭县| 大余县| 环江| 玉溪市| 临朐县|