标签:
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; import javax.servlet.annotation.WebFilter; import javax.servlet.annotation.WebInitParam; @WebFilter(urlPatterns = "/*", initParams = { @WebInitParam(name = "encoding", value = "UTF-8") }) public class EncodingFilter implements Filter {
//设置默认编码为UTF-8 private String charset = "UTF-8"; @Override public void destroy() { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { request.setCharacterEncoding(this.charset); response.setCharacterEncoding(this.charset); chain.doFilter(request, response); } @Override public void init(FilterConfig config) throws ServletException { if (config.getInitParameter("encoding") != null) { this.charset = config.getInitParameter("encoding"); } } }
标签:
原文地址:http://www.cnblogs.com/denied/p/5467136.html