该死的!不得不让我说脏话了!莫名其妙的问题。现在算是比较清楚了。最近正在做一个分布式的系统的整合。中心数据库用的utf8的编码,以前的老的系统用的是latin1的编码。在latin1的编码中插入和查询数据:不用在连接字符上花功夫。只要下面这个类,把中文转换为latin1的编码,因为默认的连接是lanti1的所以还要另外的连接字符串吗?
<filter> <filter-name>filter-name</filter-name> <filter-class>com.motel168.util.SetEncodeFilter</filter-class> <init-param> <param-name>defaultencoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>filter-name</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
package com.motel168.util; 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 SetEncodeFilter implements Filter { protected FilterConfig filterConfig = null; protected String defaultEncoding = null; public void init(FilterConfig arg0) throws ServletException { this.filterConfig = arg0; this.defaultEncoding = filterConfig.getInitParameter("defaultencoding"); } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { request.setCharacterEncoding(selectEncoding(request)); chain.doFilter(request, response); } public void destroy() { this.defaultEncoding = null; this.filterConfig = null; } protected String selectEncoding(ServletRequest request) { return this.defaultEncoding; } }
Powered by: BlogJava Copyright © qiyadeng