<filter-name>EncodingFilter</filter-name>
<filter-class>com.fzw.util.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gbk</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2.EncodingFilter.java:
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 EncodingFilter implements Filter{
private String encoding = "GBK";
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
request.setCharacterEncoding(this.encoding);
filterChain.doFilter(request, response);
}
public void init(FilterConfig config) throws ServletException {
String encoding = config.getInitParameter("encoding");
if(encoding != null && encoding.trim().length()>0){
this.encoding = encoding;
}
}
}