(1)對mysql的my.ini配置文件的默認編碼(好像是default-char-set)utf8
重啟mysql服務。
(2)web.xml配置過濾器,這個應該很簡單,copy就OK了
Xml代碼
<filter>
<filter-name>
SetWebApplicationCharacterEncoding
</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>
SetWebApplicationCharacterEncoding
</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>
字符串過濾類
Java代碼
importjava.io.IOException;
importjavax.servlet.Filter;
importjavax.servlet.FilterChain;
importjavax.servlet.FilterConfig;
importjavax.servlet.ServletException;
importjavax.servlet.ServletRequest;
importjavax.servlet.ServletResponse;
publicclassSetEncodeFilterimplementsFilter{
protectedFilterConfigfilterConfig=null;
protectedStringdefaultEncoding=null;
publicvoidinit(FilterConfigarg0)throwsServletException{
this.filterConfig=arg0;
this.defaultEncoding=filterConfig.getInitParameter("defaultencoding");
}
publicvoiddoFilter(
ServletRequestrequest,
ServletResponseresponse,
FilterChainchain)
throwsIOException,ServletException{
request.setCharacterEncoding("UTF-8");
chain.doFilter(request,response);
}
publicvoiddestroy(){
this.defaultEncoding=null;
this.filterConfig=null;
}
}