隨筆 - 100  文章 - 50  trackbacks - 0
          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          收藏夾

          我收藏的一些文章!

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          1.數據庫建立為UTF-8格式
          2.項目右鍵屬性為UTF-8格式
          3.所有頁面申明為UTF-8
          4.JDBC URL設為:UTF-8
          jdbc:mysql://localhost:3306/company?useUnicode=true&characterEncoding=utf-8
          5.數據庫Driver選擇UTF-8格式
          6.Tomcat編碼改成UTF-8
          在server.xml里面增加URIEncoding="UTF-8"
          Xml代碼 復制代碼
          1. <Connector port="8080" maxHttpHeaderSize="8192" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="UTF-8" />  

          7.加編碼過濾器SetCharacterEncodingFilter.java 
          注意:根據所在包名確定具體使用哪個包
          Java代碼 復制代碼
          1. import java.io.IOException;   
          2.   
          3. import javax.servlet.Filter;   
          4.   
          5. import javax.servlet.FilterChain;   
          6.   
          7. import javax.servlet.FilterConfig;   
          8.   
          9. import javax.servlet.ServletException;   
          10.   
          11. import javax.servlet.ServletRequest;   
          12.   
          13. import javax.servlet.ServletResponse;   
          14.   
          15. public class SetCharacterEncodingFilter implements Filter {   
          16.   
          17.     protected String encoding = null;   
          18.   
          19.     protected FilterConfig filterConfig = null;   
          20.   
          21.     protected boolean ignore = true;   
          22.   
          23.     public void init(FilterConfig filterConfig) throws ServletException {   
          24.   
          25.         this.filterConfig = filterConfig;   
          26.   
          27.         this.encoding = filterConfig.getInitParameter("encoding");   
          28.   
          29.         String value = filterConfig.getInitParameter("ignore");   
          30.   
          31.         if (value == null)   
          32.   
          33.             this.ignore = true;   
          34.   
          35.         else if (value.equalsIgnoreCase("true"))   
          36.   
          37.             this.ignore = true;   
          38.   
          39.         else if (value.equalsIgnoreCase("yes"))   
          40.   
          41.             this.ignore = true;   
          42.   
          43.         else  
          44.   
          45.             this.ignore = false;   
          46.   
          47.     }   
          48.   
          49.     public void doFilter(ServletRequest request, ServletResponse response,   
          50.             FilterChain chain) throws IOException, ServletException {   
          51.   
          52.         // TODO 自動生成方法存根   
          53.   
          54.         if (ignore || (request.getCharacterEncoding() == null)) {   
          55.   
          56.             String encoding = selectEncoding(request);   
          57.   
          58.             if (encoding != null)   
          59.   
          60.                 request.setCharacterEncoding(encoding);   
          61.   
          62.         }   
          63.   
          64.         chain.doFilter(request, response);   
          65.   
          66.     }   
          67.   
          68.     public void destroy() {   
          69.   
          70.         // TODO 自動生成方法存根   
          71.   
          72.         this.encoding = null;   
          73.   
          74.         this.filterConfig = null;   
          75.   
          76.     }   
          77.   
          78.     protected String selectEncoding(ServletRequest request) {   
          79.   
          80.         return (this.encoding);   
          81.   
          82.     }   
          83.   
          84. }  


          在web.xml里面映射配置
          Xml代碼 復制代碼
          1. <filter>  
          2.   <filter-name>Set Character Encoding</filter-name>  
          3.   <filter-class>com.yourcompany.util.SetCharacterEncodingFilter</filter-class>  
          4.   <init-param>  
          5.     <param-name>encoding</param-name>  
          6.     <param-value>UTF-8</param-value>  
          7.   </init-param>  
          8. </filter>  
          9. <filter-mapping>  
          10.   <filter-name>Set Character Encoding</filter-name>  
          11.   <url-pattern>/*</url-pattern>  
          12. </filter-mapping> 
          posted on 2010-06-03 22:33 fly 閱讀(436) 評論(0)  編輯  收藏 所屬分類: jsp學習
          主站蜘蛛池模板: 巩义市| 永顺县| 鲁甸县| 白水县| 嘉祥县| 个旧市| 哈巴河县| 宁夏| 鄄城县| 连城县| 醴陵市| 定兴县| 潮安县| 抚顺县| 酉阳| 神农架林区| 夏邑县| 隆德县| 霍邱县| 桑植县| 洪洞县| 金溪县| 精河县| 洮南市| 长沙县| 岑溪市| 聂荣县| 灵台县| 大兴区| 鄂伦春自治旗| 会同县| 西峡县| 剑阁县| 张家界市| 武宣县| 常宁市| 新巴尔虎右旗| 青阳县| 四子王旗| 女性| 丰都县|