Garbled is a more common problem in the Java EE. If you encounter one or two problems, you can use the new String (Request.getparameter (XXX). GetBytes ("Iso-8859-1"), "UTF-8") to resolve. When you encounter many situations, it is best to use a filter.
The filter only needs to notice 2 places--Class and Web.xml
1. The publication of the above Web.xml is as follows:
<fileter>
<!--class name-->
<filter-name>SetCharsetEncodingFilter</filter-name>
<!---Class path-->
<filter-class>SetCharacter</filter-class>
<init-param>
< param-name>encoding</param-name>
<param-value>utf-8</param-value>
</ init-param>
<filter-mapping>
<filter-name>setcharsetencodingfilter</filter-name >
<!--setup All files encountered filters must be intercepted-->
<url-pattern>/*</url-pattern>
</ Filter-mapping>
</fileter>
2. Filtration Class
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 Setcharacter implements Filter {protected String encoding = NULL;
protected Filterconfig filterconfig = null;
Protected Boolean ignore = true;
public void init (Filterconfig arg0) throws servletexception {this.encoding = Arg0.getinitparameter ("encoding");
String value = Arg0.getinitparameter ("Imnore");
if (value = = null) {This.ignore = true;
else if (Value.equalsignorecase ("true") {This.ignore = true;
else if (value.equalsignorecase ("yes")) {This.ignore = true; } public void Dofilter (ServletRequest arg0, Servletresponse arg1, Filterchain arg2) throws IOException, S ervletexception {if (Ignore | | (arg0.getcharacterencoding () = null)) {String encoding = selectencoding (arg0);
if (encoding!= NULL) arg0.setcharacterencoding (encoding);
} arg2.dofilter (arg0, arg1);
Private String selectencoding (ServletRequest arg0) {return (this.encoding);
public void Destroy () {this.encoding = null;
This.filterconfig = null; }
}
In the Web.xml file, the following syntax is used to define mappings:
1, the "/" at the beginning and the "/*" End is used to do path mapping.
2, before the prefix "*." The beginning is used to do extension mapping.
3, with "/" is used to define the default servlet mapping.
4, the rest is used to define detailed mapping. For example:/aa/bb/cc.action
The above is to solve the Java EE garbled problem of ideas, to share with everyone, I hope that you encounter similar problems can be successfully resolved.