隨筆 - 100  文章 - 50  trackbacks - 0
          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          收藏夾

          我收藏的一些文章!

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          1.數(shù)據(jù)庫建立為UTF-8格式
          2.項目右鍵屬性為UTF-8格式
          3.所有頁面申明為UTF-8
          4.JDBC URL設(shè)為:UTF-8
          jdbc:mysql://localhost:3306/company?useUnicode=true&characterEncoding=utf-8
          5.數(shù)據(jù)庫Driver選擇UTF-8格式
          6.Tomcat編碼改成UTF-8
          在server.xml里面增加URIEncoding="UTF-8"
          Xml代碼 復(fù)制代碼
          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 
          注意:根據(jù)所在包名確定具體使用哪個包
          Java代碼 復(fù)制代碼
          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代碼 復(fù)制代碼
          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 閱讀(442) 評論(0)  編輯  收藏 所屬分類: jsp學(xué)習(xí)
          主站蜘蛛池模板: 郴州市| 田林县| 武乡县| 揭西县| 定日县| 卫辉市| 曲麻莱县| 永安市| 桓台县| 隆回县| 乌鲁木齐市| 中阳县| 宣化县| 新晃| 城步| 申扎县| 通渭县| 剑川县| 东阿县| 漳浦县| 西和县| 广元市| 德兴市| 井陉县| 平山县| 遂宁市| 武乡县| 清镇市| 阿城市| 威海市| 兖州市| 体育| 淮滨县| 泊头市| 嘉祥县| 金堂县| 六盘水市| 文化| 黄骅市| 汉沽区| 象山县|