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>

          主站蜘蛛池模板: 佛山市| 赞皇县| 嘉黎县| 陇川县| 堆龙德庆县| 泰安市| 融水| 平果县| 朔州市| 胶州市| 沅江市| 比如县| 错那县| 庆城县| 彭阳县| 曲水县| 阳西县| 汤阴县| 五莲县| 泰兴市| 灵武市| 广水市| 黎川县| 庄浪县| 桂平市| 温州市| 嘉义县| 应城市| 孙吴县| 晋宁县| 汤阴县| 西和县| 扎兰屯市| 那曲县| 神木县| 台北县| 乌海市| 明水县| 汶上县| 惠水县| 弥渡县|