码迷,mamicode.com
首页 > 其他好文 > 详细

利用Filter技术解决中文乱码的简单实现

时间:2015-08-14 18:50:08      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:

web.xml的配置

<filter>
        <filter-name>FilterTest01</filter-name>
        <filter-class>com.baowei.fiiter.FilterTest01</filter-class>
        <init-param>
            <param-name>charset</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>FilterTest01</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

Filter的实现

public class FilterTest01 implements Filter {

    private FilterConfig config = null;
    private String defaultCharSet = "utf-8";

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        config = filterConfig;
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {

        String charset = config.getInitParameter("charset");
        if (charset == null) {
            charset = defaultCharSet;
        }

          HttpServletRequest req = (HttpServletRequest) request;
          HttpServletResponse resp = (HttpServletResponse) response;


          req.setCharacterEncoding(charset);
          resp.setCharacterEncoding(charset);
          resp.setContentType("text/html;charset=" + charset);

 

    }

    @Override
    public void destroy() {

    }

}

 

利用Filter技术解决中文乱码的简单实现

标签:

原文地址:http://www.cnblogs.com/zhangbaowei/p/4730739.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!