Attention to the problem: in learning to use Selvert filter to handle Chinese garbled, in the filter configuration initialization with the Utf-8 processing Chinese garbled, and in the submission of the JSP page is used GBK. Although both can come out of Chinese garbled, but it caused the processing of garbled format inconsistency. So there was a compilation error.
Solution: Use Utf-8 or GBK everywhere
Copy Code code as follows:
Filter class
charactorfilter.jsp
Package cn.com.Filter;
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 Charactorfilter implements filter {//Inheritance filter class
Character encoding
String Encoding=null;
public void Dofilter (ServletRequest request, servletresponse response,
Filterchain chain) throws IOException, Servletexception {
if (encoding!=null) {
Set Request character encoding
request.setcharacterencoding (encoding);
Set Response character encoding
Response.setcontenttype ("text/html;charset=" +encoding);
}
Pass to next filter
Chain.dofilter (request, response);
}
public void init (Filterconfig filterconfig) throws Servletexception {
Get initialization parameters
Encoding=filterconfig.getinitparameter ("encoding");
}
public void Destroy () {
TODO auto-generated Method Stub
Encoding=null;
}
}
Xml
Copy Code code as follows:
<filter> <!--Note that this is filter, do not configure as Servlet-->
<filter-name>CharactorFilter</filter-name> <!--filter name-->
<filter-class>cn.com.Filter.CharactorFilter</filter-class> <!--filter Complete class name-->
<init-param> <!--initialization parameters-->
<param-name>encoding</param-name> <!--parameter name-->
<param-value>utf-8</param-value> <!--parameter value-->
</init-param>
</filter>
<filter-mapping> <!--filter mapping-->
<filter-name>CharactorFilter</filter-name><!--Filter Name-->
<url-pattern>/*</url-pattern><!--URL mapping, to deal with garbled--> for all pages
</filter-mapping>