thinkthink's bolg

          enjoy

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            0 Posts :: 3 Stories :: 0 Comments :: 0 Trackbacks

          一. 亂碼問題是web開發過程中基本都要處理的。
          二.解決辦法:

           1。 統一全部的編碼方式,強烈建議采用“UTF-8”;
               
             例如,jsp 里面的配置為
          1<%@ page contentType="text/html;charset=UTF-8"  pageEncoding="UTF-8" %>
            2。 對需要在web.xml中配置過濾器(filter)的,也要設置為“UTF-8”;
             例如:
              
          package commons.filter;

          import javax.servlet.*;
          import javax.servlet.http.*;
          import java.io.*;
          import java.util.*;

          public class EncodingFilter extends HttpServlet implements Filter {
              
          private FilterConfig filterConfig;
              
          private String targetEncoding = "UTF-8";
            //Handle the passed-in FilterConfig
              public void init(FilterConfig filterConfig) throws ServletException {
                  
          this.filterConfig = filterConfig;
              }


              
          //Process the request/response pair
              public void doFilter(ServletRequest request, ServletResponse response,
                                   FilterChain filterChain) 
          {
                  
          try {
                      request.setCharacterEncoding(targetEncoding);
                      filterChain.doFilter(request, response);
                  }
           catch (ServletException sx) {
                      filterConfig.getServletContext().log(sx.getMessage());
                  }
           catch (IOException iox) {
                      filterConfig.getServletContext().log(iox.getMessage());
                  }

              }


              
          //Clean up resources
              public void destroy() {
                  filterConfig 
          = null;
                  targetEncoding 
          = null;
              }

          }

          3. 如果不用“UTF-8”,你會發現在使用AJAX提交數據時,URL中的中文在服務器端是亂碼,因為AJAX是以“UTF-8”編碼提交數據的。
          4.關于國際化屬性文件(xxxxx.properties), 因為需要unicode編碼,建議使用PropertiesEditor國際化插件進行編輯,他會自動存為unicode編碼。
              關于這個PropertiesEditor國際化插件,我下載的時候發現只有for eclipse3.0的, 如果用的eclipse版本是3.2,,3.3根本不行,于是只有下載standone 方式的PropertiesEditor,在cmd下運行: java -jar PropertiesEditor.jar 

          5.如果發現數據庫數據也有亂碼,記得正確設置數據庫的編碼方式
          posted on 2007-08-25 23:55 thinkthink 閱讀(325) 評論(0)  編輯  收藏 所屬分類: Struts2

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


          網站導航:
           
          主站蜘蛛池模板: 山阳县| 蒙城县| 宝坻区| 望城县| 河间市| 崇礼县| 兴宁市| 乐东| 永康市| 定安县| 鲁甸县| 塘沽区| 台东市| 绥化市| 五大连池市| 镇安县| 河池市| 平凉市| 麻江县| 尉氏县| 宁津县| 台安县| 呼图壁县| 千阳县| 耒阳市| 临桂县| 晋城| 汉阴县| 左云县| 抚州市| 遵化市| 溧水县| 广东省| 门源| 全椒县| 沾化县| 景洪市| 腾冲县| 巫溪县| 晴隆县| 陆良县|