ply

          吞噬黑暗
          posts - 1, comments - 11, trackbacks - 0, articles - 13
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          采用過濾器解決中文亂碼問題

          Posted on 2011-08-21 23:13 ply 閱讀(677) 評論(0)  編輯  收藏 所屬分類: 知識小串通

          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 CharacterEncodingFilter implements Filter{

              private FilterConfig config;
              private String encoding;
              private boolean enable;
             
              public void destroy() {
                 
              }

              public void doFilter(ServletRequest request, ServletResponse response,
                      FilterChain filterChain) throws IOException, ServletException {
                 
                  if(this.enable){
                      request.setCharacterEncoding(encoding);
                      response.setCharacterEncoding(encoding);

                  }
                 
                  filterChain.doFilter(request, response);
                 
                 
              }

              public void init (FilterConfig config ) throws ServletException {
                  this.config = config;
                  loadConfigParams();
                 
                 
              }

              private void loadConfigParams() {
                  this.encoding= this.config.getInitParameter("enconding");
                  String flag = this.config.getInitParameter("enable");
                  if(flag.equalsIgnoreCase("true")){
                      this.enable = true;
                  }else{
                      this.enable = false;
                  }
                 
                 
              }

          }

           

           

           

          web.xml

           

          <filter>
              <filter-name>encondingFilter</filter-name>
              <filter-class>com.morning.web.filter.CharacterEncodingFilter</filter-class>
              <init-param>
                  <param-name>enconding</param-name>
                  <param-value>utf-8</param-value>
              </init-param>
              <init-param>
                  <param-name>enable</param-name>
                  <param-value>true</param-value>
              </init-param>
          </filter>


          <filter-mapping>
              <filter-name>encondingFilter</filter-name>
              <url-pattern>*.do</url-pattern>
          </filter-mapping>

          主站蜘蛛池模板: 延寿县| 山东| 团风县| 西贡区| 宜宾县| 嵊泗县| 都安| 延津县| 乌兰察布市| 揭东县| 江口县| 思茅市| 赫章县| 上犹县| 农安县| 西乌珠穆沁旗| 宣威市| 新蔡县| 启东市| 玛沁县| 新化县| 图们市| 和田县| 天长市| 沙雅县| 和龙市| 永寿县| 乌拉特后旗| 察哈| 修水县| 蒙山县| 湄潭县| 富蕴县| 分宜县| 舞阳县| 陆河县| 思茅市| 姚安县| 四川省| 海原县| 竹北市|