First write the filter code in the SRC directory
Package filter;
Import javax.servlet.*;
Import javax.servlet.http.*;
Import java.io.*;
public class Myfilter implements Filter {
Private Filterconfig FC;
public void Init (Filterconfig FC) throws Servletexception {
TODO auto-generated Method Stub
THIS.FC=FC;
}
public void Dofilter (ServletRequest request, servletresponse response, Filterchain chain) throws IOException, servletexception {
TODO auto-generated Method Stub
Request.setcharacterencoding ("gb2312");
Response.setcharacterencoding ("gb2312");
Chain.dofilter (Request,response);
}
public void Destroy () {
TODO auto-generated Method Stub
}
}
Configuration in Web.xml:
<filter>
<filter-name>set Character encoding</filter-name>
<filter-class>com. Xx. Xx.listener.setcharacterencodingfilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>set Character encoding</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>
<filter-mapping>
<filter-name>set Character encoding</filter-name>
<url-patten>/*</url-patten>
</filter-mapping>