In the past two days, I created a project JSP + hibernate + MySQL. During the query, the Chinese language was normal, but the Chinese data was inserted with garbled characters. So I found a filter on the Internet to solve the problem, in JSP, gb2312 is used, and in MySQL, gb2312. write a filter as follows:
Package filter;
Import javax. servlet .*;
Import javax. servlet. http .*;
Import java. Io .*;
Public class characterencodingfilter implements filter ...{
Private filterconfig;
Private string encoding = NULL;
Public void Init (filterconfig )...{
This. filterconfig = filterconfig;
Encoding = filterconfig. getinitparameter ("encoding ");
}
Public void dofilter (servletrequest request, servletresponse response, filterchain chain) throws ioexception, servletexception ...{
Request. setcharacterencoding (encoding );
Chain. dofilter (request, response );
}
Public void destroy ()...{
}
}
Add filter to Web. xml:
<Filter>
<Filter-Name> setcharacterencodingfilter </filter-Name>
<Filter-class> filter. characterencodingfilter </filter-class>
<Init-param>
<Param-Name> encoding </param-Name>
<Param-value> gb2312 </param-value>
</Init-param>
</Filter>
<Filter-mapping>
<Filter-Name> setcharacterencodingfilter </filter-Name>
<URL-pattern>/* </url-pattern>
</Filter-mapping>
In this way, the Chinese garbled characters in JSP can be solved.