Garbled characters occur when the jsp tutorial is used to pass Chinese parameters to the servlet at the backend. After reading the code on the Internet, there are many codec methods available and there are many other methods. Finally, this method is the simplest.
Open tomcat server. xml
Add the blue part!
<Connection port = "8080"
Maxthreads = "150" minsparethreads = "25" maxsparethreads = "75"
Enablelookups tutorial = "false" redirectport = "8443" acceptcount = "100"
Debug = "0" connectiontimeout = "20000"
Disableuploadtimeout = "true" uriencoding = "UTF-8 or gbk"/>
Test instance code
Java
Public class setcharacterencodingfilter implements filter
{
Protected string encoding = null;
Protected filterconfig = null;
Protected boolean ignore = true;
Public void destroy ()
{
This. encoding = null;
This. filterconfig = null;
}
Public void dofilter (servletrequest request, servletresponse response, filterchain chain) throws ioexception, servletexception
{
If (ignore | (request. getcharacterencoding () = null ))
{
String encoding = selectencoding (request );
If (encoding! = Null)
Request. setcharacterencoding (encoding );
}
Chain. dofilter (request, response );
}
Public void init (filterconfig) throws servletexception
{
This. filterconfig = filterconfig;
This. encoding = filterconfig. getinitparameter ("encoding ");
String value = filterconfig. getinitparameter ("ignore ");
If (value = null)
This. ignore = true;
Else if (value. Inclusignorecase ("true "))
This. ignore = true;
Else if (value. Inclusignorecase ("yes "))
This. ignore = true;
Else
This. ignore = false;
}
Protected string selectencoding (servletrequest request)
{
Return (this. encoding );
}
}
Web. xml configuration
<
Filter>
<Filter-name> encodingfilter </filter-name>
<Filter-class> com. dfgg. sms. web. setcharacterencodingfilter </filter-class>
<Init-param>
<Param-name> encoding </param-name>
<Param-value> UTF-8 </param-value>
</Init-param>
</Filter>
<Filter-mapping>
<Filter-name> encodingfilter </filter-name>
<Url-pattern>/* </url-pattern>
</Filter-mapping>