首页 > 其他 > 详细

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

时间:2015-08-14 18:49:55      阅读:170      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!