JSP code:
Import javax.servlet.*;
Import javax.servlet.http.*; public class Charsetfilter implements Filter {public void Destroy () {} public void Dofilter (ServletRequest Request, servletresponse response, Filterchain chain) {try {httpservletrequest HttpRequest = (httpserv
letrequest) Request;
String method = Httprequest.getmethod (). toLowerCase ();
if (Method.equals ("POST")) {//If it is post, that is, form method, directly set CharSet can request.setcharacterencoding ("UTF-8");
else if (method.equals ("get")) {//If the Get method request.setcharacterencoding ("UTF-8"); Request = new Httpservletrequestwrapper ((httpservletrequest) request) {public String getparameter (Str ing str) {try {return new String (Super.getparameter (str). GetBytes ("Iso-8
859-1 ")," GBK ");
catch (Exception e) {return null;
} }
};
Chain.dofilter (request, response); catch (Exception e) {}} public void init (Filterconfig filterconfig) {}}
Filter configuration:
<filter>
<filter-name>CharFilter</filter-name>
<filter-class>CharsetFilter< /filter-class>
</filter>
<filter-mapping>
<filter-name>charfilter</ filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>