小菜毛毛技術分享

          與大家共同成長

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            164 Posts :: 141 Stories :: 94 Comments :: 0 Trackbacks
          亂碼是j2ee中一個比較常見的問題。遇到1,2個情況下,可以用new String(request.getParameter(xxx).getBytes("ISO-8859-1"),"UTF-8")來解決。遇到多的情況下,就最好用過濾器了。
          1個過濾器只需要注意2個地方,類和在web.xml上面的發布。

          1。在web.xml上面的發布如下:
          <filter>
                
          <filter-name>SetCharsetEncodingFilter</filter-name //這個是類名
                
          <filter-class>org.SetCharacter</filter-class>        //這個是類的位置
                
          <init-param>
                    
          <param-name>encoding</param-name>
                    
          <param-value>utf-8</param-value>
                
          </init-param>
            
          </filter>
            
          <filter-mapping>
                
          <filter-name>SetCharsetEncodingFilter</filter-name>
                
          <url-pattern> /* </url-pattern>   //這個代表所有的文件遇到過濾器都要被攔截
            </filter-mapping>
          注意配置文件之中:<filter-class> com.struts.common.SetCharacterEncodingFilter </filter-class>這是文件的位置
          2。過濾的這個類如下:
          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   SetCharacterEncodingFilter   implements   Filter   
          protected   String   encoding   =   null
          protected   FilterConfig   filterConfig   =   null
          protected   boolean   ignore   =   true

          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 
          this.ignore=false
          }
           

          public   void   doFilter(ServletRequest   request,   ServletResponse   response,   FilterChain   chain)   throws   IOException,   ServletException   
          //   TODO   自動生成方法存根 
          if   (ignore   ||   (request.getCharacterEncoding()   ==   null))   
          String   encoding   
          =   selectEncoding(request); 
          if   (encoding   !=   null
          request.setCharacterEncoding(encoding); 
          }
           
          chain.doFilter(request,   response); 
          }
           

          public   void   destroy()   
          //   TODO   自動生成方法存根 
          this.encoding   =   null
          this.filterConfig   =   null
          }
           

          protected   String   selectEncoding(ServletRequest   request)   
          return   (this.encoding); 
          }
           
          }
           

          注意:COPY我的代碼的時候記得吧字符串變量后的空格去掉
          posted on 2010-01-11 09:41 小菜毛毛 閱讀(338) 評論(0)  編輯  收藏 所屬分類: J2EE相關技術與框架
          主站蜘蛛池模板: 宣汉县| 酒泉市| 丰县| 乐业县| 罗甸县| 汉源县| 玉门市| 洞头县| 中山市| 江华| 库伦旗| 千阳县| 屏东县| 调兵山市| 济宁市| 南靖县| 平和县| 上杭县| 津市市| 岱山县| 五台县| 融水| 黔西县| 仙桃市| 襄樊市| 峨边| 新泰市| 方正县| 高要市| 长海县| 柯坪县| 南康市| 水城县| 苗栗市| 清丰县| 都昌县| 扬中市| 辉县市| 彭州市| 金沙县| 清新县|