过滤器的使用-字符编码

时间:2021-02-02 11:03:29   收藏:0   阅读:0
import javax.servlet.*;
import java.io.IOException;

public class CharacterEncodingFilter implements Filter {

    private static String encoding; // 定义变量接收初始化的值

    @Override
    public void init(FilterConfig config) throws ServletException {
        encoding = "UTF-8";
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        request.setCharacterEncoding(encoding);
        response.setCharacterEncoding(encoding);
//        导致css加载不生效
//        response.setContentType("text/html;charset="+encoding); //设置response字符编码
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {

    }
}
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!