写一个拦截器类来做request和response的编码过滤:
public class CharsetFilter implements Filter{
private String encoding;
@Override
public void destroy() { }
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding(encoding);
response.setCharacterEncoding(encoding);
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
encoding = filterConfig.getInitParameter("encoding");
}
}
然后在xml文件中配置:
CharSetFilter
com.filter.CharsetFilter
encoding
UTF-8
CharSetFilter
/*