1. Java class: characterencodingfilter
Import javax. servlet .*;
Import java. Io. ioexception;
Public class characterencodingfilter implements filter {
Private string encoding = "UTF-8 ";
Private Boolean forceencoding = true;
/**
* Set the encoding to use for requests. This encoding will be
* Passed into a servletrequest. setcharacterencoding call.
* <P> whether this encoding will override existing request
* Encodings depends on the "forceencoding" flag.
*/
Public void setencoding (string encoding ){
This. Encoding = encoding;
}
/**
* Set whether the encoding of this filter shocould override existing
* Request encodings. Default is "false", I. e. Do not modify Encoding
* If servletrequest. getcharacterencoding returns a non-null value.
*/
Public void setforceencoding (Boolean forceencoding ){
This. forceencoding = forceencoding;
}
Public void Init (filterconfig) throws servletexception {
This. Encoding = filterconfig. getinitparameter ("encoding ");
String force = filterconfig. getinitparameter ("forceencoding ");
This. forceencoding = (force = NULL) | Boolean. valueof (force). booleanvalue ();
}
Public void dofilter (servletrequest,
Servletresponse,
Filterchain) throws ioexception, servletexception {
If (this. forceencoding | servletrequest. getcharacterencoding () = NULL ){
Servletrequest. setcharacterencoding (this. Encoding );
}
Filterchain. dofilter (servletrequest, servletresponse );
}
Public void destroy (){
}
}
2. Add filters configurations to the Web. xml configuration file.
<! -- Filters configurations -->
<Filter>
<Filter-Name> character_encoding </filter-Name>
<Filter-class>
Common. encoding. characterencodingfilter </filter-class>
<Init-param>
<Param-Name> encoding </param-Name>
<Param-value> UTF-8 </param-value>
</Init-param>
<Init-param>
<Param-Name> forceencoding </param-Name>
<Param-value> true </param-value>
</Init-param>
</Filter>
<Filter-mapping>
<Filter-Name> character_encoding </filter-Name>
<URL-pattern>/* </url-pattern>
</Filter-mapping>
3. The database is created in UTF-8 format
4. Right-click Project Properties in UTF-8 format
5. All page declarations as UTF-8
6. jdbc url is set to: UTF-8
JDBC: mysql: // localhost: 3306/databaseuse? Useunicode = true & characterencoding = UTF-8
7. Database driver select UTF-8 format
8. Tomcat encoding into UTF-8
Add uriencoding = "UTF-8" in server. xml"
<Connector Port = "8080" maxhttpheadersize = "8192" maxthreads = "150" minsparethreads = "25" maxsparethreads = "75" enablelookups = "false" redirectport = "8443" acceptcount =" 100 "connectiontimeout =" 20000 "disableuploadtimeout =" true "uriencoding =" UTF-8 "/>