utf-8 中文乱码解决方案

1.java类: CharacterEncodingFilter

import javax.servlet.*;
import java.io.IOException;

public class CharacterEncodingFilter implements Filter {

    private String encoding = "UTF-8";
    private boolean forceEncoding = true;

    /**
     * Set the encoding to use for requests. This encoding will be
     * passed into a ServletRequest.setCharacterEncoding call.
     * <p>Whether this encoding will override existing request
     * encodings depends on the "forceEncoding" flag.
     */
    public void setEncoding(String encoding) {
        this.encoding = encoding;
    }

    /**
     * Set whether the encoding of this filter should override existing
     * request encodings. Default is "false", i.e. do not modify encoding
     * if ServletRequest.getCharacterEncoding returns a non-null value.
     */
    public void setForceEncoding(boolean forceEncoding) {
        this.forceEncoding = forceEncoding;
    }

    public void init(FilterConfig filterConfig) throws ServletException {
        this.encoding = filterConfig.getInitParameter("encoding");
        String force = filterConfig.getInitParameter("forceEncoding");
        this.forceEncoding = (force == null) || Boolean.valueOf(force).booleanValue() ;
    }

    public void doFilter(ServletRequest servletRequest,
                         ServletResponse servletResponse,
                         FilterChain filterChain) throws IOException, ServletException {
        if (this.forceEncoding || servletRequest.getCharacterEncoding() == null) {
            servletRequest.setCharacterEncoding(this.encoding);
        }
        filterChain.doFilter(servletRequest, servletResponse);
    }

    public void destroy() {

    }

}


2.在web.xml配置文件里加Filters Configurations

<!-- Filters Configurations 	-->
	<filter>
		<filter-name>CHARACTER_ENCODING</filter-name>
		<filter-class>
			common.encoding.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CHARACTER_ENCODING</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>



3.数据库建立为UTF-8格式
4.项目右键属性为UTF-8格式
5.所有页面申明为UTF-8
6.JDBC URL设为:UTF-8
jdbc:mysql://localhost:3306/databaseuse?useUnicode=true&characterEncoding=utf-8
7.数据库Driver选择UTF-8格式
8.Tomcat编码改成UTF-8

在server.xml里面增加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" />




在URL中传递参数,是通过HTTP报头来传递的.并不是类似于通过表单来传递,表单中传递的参数是在HTTP中的BODY中传送的.而设置字符过滤器只能起到过滤HTTP中BODY中的参数.而TOMCAT默认是ISO8859-1的编码方式,所以你在URL中传递过去后就是乱码了.而且过滤器也不管用了. 解决方法.直接改变TOMCAT的默认字符编码方式: tomcat做web服务器时,通过地址栏传中文参数时,为防止浏览器上的为乱码 需在服务器server.xml中配置对应的端口中配置编码方式即可.例如:    
<Connector port="8080"    maxThreads="150" minSpareThreads="25" maxSpareThreads="75"    enableLookups="false" redirectPort="8443" acceptCount="100"    debug="0" connectionTimeout="20000"   disableUploadTimeout="true"  useBodyEncodingForURI="true" URIEncoding="UTF-8"/> 


中加入 useBodyEncodingForURI="true" URIEncoding="UTF-8"就可以了.


你可能感兴趣的:(java,tomcat,mysql,xml,jdbc)