(1)对mysql的my.ini配置文件的默认编码(好像是default-char-set)utf8
重启mysql服务。
(2)web.xml配置过滤器,这个应该很简单,copy就OK了
- <filter>
- <filter-name>
- Set Web Application Character Encoding
- </filter-name>
- <filter-class>*.*.SetEncodeFilter</filter-class> <!--这里填写你的包-->
- <init-param>
- <param-name>defaultencoding</param-name>
- <param-value>UTF-8</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>
- Set Web Application Character Encoding
- </filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
<filter> <filter-name> Set Web Application Character Encoding </filter-name> <filter-class>*.*.SetEncodeFilter</filter-class> <!--这里填写你的包--> <init-param> <param-name>defaultencoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name> Set Web Application Character Encoding </filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
字符串过滤类
- 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("UTF-8");
- chain.doFilter(request, response);
- }
- public void destroy() {
- this.defaultEncoding = null;
- this.filterConfig = null;
- }
- }