posts - 403, comments - 310, trackbacks - 0, articles - 7
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          Web Project 3 亂碼解決方案小結(jié)

          Posted on 2007-05-15 19:38 ZelluX 閱讀(362) 評(píng)論(0)  編輯  收藏 所屬分類: OOP
          1. 數(shù)據(jù)庫亂碼
          使用EMS SQL Manager for MySQL可以容易地解決這個(gè)問題,在table的屬性對(duì)話框中將Character Set設(shè)為GBK即可。

          2. Ajax應(yīng)用中Servlet返回內(nèi)容亂碼
          設(shè)置響應(yīng)報(bào)頭的編碼即可
          response.setHeader("Content-Type", "text/html;charset=GB2312");

          3. JSP頁面顯示亂碼
          在頁面開頭加上
          <%@ page pageEncoding="GBK"%>

          4. request.getParameter產(chǎn)生亂碼
          用post方法只要在獲取參數(shù)前設(shè)置編碼即可
          request.setCharacterEncoding("GBK");
          get方法還需要在獲取參數(shù)后轉(zhuǎn)換編碼
          name = new String(name.getBytes("ISO8859-1"), "GBK");

          5. google到的另一個(gè)解決方法,不過貌似不成功 -,-
          package filter;

          import javax.servlet.Filter;
          import javax.servlet.FilterConfig;
          import javax.servlet.ServletException;
          import javax.servlet.ServletRequest;
          import javax.servlet.ServletResponse;
          import javax.servlet.FilterChain;
          import java.io.IOException;

          public class EncodingFilter implements Filter {
            
          protected FilterConfig filterConfig;

            
          protected String encodingName;

            
          protected boolean enable;

            
          public EncodingFilter() {
              
          this.encodingName = "UTF-8";
              
          this.enable = false;
            }


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


            
          private void loadConfigParams() {
              
          this.encodingName = this.filterConfig.getInitParameter("encoding");
              String strIgnoreFlag 
          = this.filterConfig.getInitParameter("enable");
              
          if (strIgnoreFlag.equalsIgnoreCase("true")) {
                
          this.enable = true;
              }
           else {
                
          this.enable = false;
              }

            }


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

              chain.doFilter(request, response);
            }


            
          public void destroy() {
            }

          }
          web.xml
           <filter>
              
          <filter-name>EncodingFilter</filter-name>
              
          <filter-class>filter.EncodingFilter</filter-class>
              
          <init-param>
                
          <param-name>encoding</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>EncodingFilter</filter-name>
              
          <url-pattern>/user/*</url-pattern>
            
          </filter-mapping>
            
          <filter-mapping>
              
          <filter-name>EncodingFilter</filter-name>
              
          <url-pattern>/manager/*</url-pattern>
            
          </filter-mapping>  
            
          主站蜘蛛池模板: 云南省| 左权县| 沂源县| 筠连县| 南江县| 阿拉善盟| 大英县| 沙雅县| 伽师县| 桃园市| 新平| 天水市| 林甸县| 兴安盟| 阿鲁科尔沁旗| 三河市| 新平| 临颍县| 林西县| 察隅县| 济阳县| 曲阳县| 永登县| 濮阳县| 阿坝| 遂川县| 岐山县| 呼伦贝尔市| 白河县| 涡阳县| 广水市| 伊吾县| 大同市| 舒兰市| 永新县| 南召县| 宝丰县| 绥宁县| 洪湖市| 竹溪县| 石台县|