In the JSP and servlet development, today I do a small example of the Chinese garbled problem, using a lot of methods, found that still can not solve. Finally, I used the filter to calculate the end.
This method is summarized as follows: Use the <%@ page pageencoding= "GB2312"%> in the JSP page, where pageencoding is GB2312, then we use the < in the filter web.xml Param-value>gb2312</param-value> also for GB2312. In a word: The above two can be consistent, such as UTF-8
I write the Java code for the filter and the configuration of the Web.xml
--------------Java code section: 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 Open, current state:" +charset);
}
}
--------------Web.xml section (the following sections can be copied and pasted directly under 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