zoukankan      html  css  js  c++  java
  • 防止出现乱码,J2EE项目统一使用UTF8编码设置方法

    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" />  
    <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. }  
    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 if (value.equalsIgnoreCase("yes")) 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); } }

    在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>
  • 相关阅读:
    10.18.2 linux文件压缩与打包
    动态代理与静态代理的区别
    JAVA的Date类与Calendar类
    【面试题001】类型转换关键字,空类对象模型,拷贝构造函数,赋值运算符函数
    【面试题001】类型转换关键字,空类对象模型,拷贝构造函数,赋值运算符函数
    j2EE经典面试题
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
  • 原文地址:https://www.cnblogs.com/danghuijian/p/4400869.html
Copyright © 2011-2022 走看看