In the development of jsp and servlet, a small example I made today shows Chinese garbled characters. I have used many methods and found that it still cannot be solved. Finally, I used a filter to end the process.
This method is summarized as follows: Use <% @ page pageEncoding = "GB2312" %> On the jsp page. The pageEncoding here is GB2312, so we will filter the web. the <param-value> GB2312 </param-value> used in xml is also GB2312. In a word: Is the above two can be consistent, such as the same as the UTF-8
I write the java code of the filter and the configuration of web. xml.
// -------------- Java code: CharsetFilter. java
Package com;
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 CharsetFilter implements Filter {
Private String charset = "GB2312 ";
Public void destroy (){
}
Public void doFilter (ServletRequest request, ServletResponse response,
FilterChain arg2) throws IOException, ServletException {
Request. setCharacterEncoding (charset );
Arg2.doFilter (request, response );
}
Public void init (FilterConfig arg0) throws ServletException {
Charset = arg0.getInitParameter ("charset ");
System. out. println ("character filter enabled, current status:" + charset );
}
}
// -------------- Web. xml section (the following section can be copied and pasted to WEB. XML)
<Filter>
<Filter-name> CharsetFilter </filter-name>
<Filter-class> com. CharsetFilter </filter-class>
<Init-param>
<Param-name> charset </param-name>
<Param-value> GB2312 </param-value>
</Init-param>
</Filter>
<Filter-mapping>
<Filter-name> CharsetFilter </filter-name>
<Url-pattern>/* </url-pattern>
</Filter-mapping>
Source: http://blog.csdn.net/lvlingwy