• servlet过滤器1 解决字符集乱码 filter


    package com.tarena.web.filter;    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.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;    import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;    public class CharacterEncodingFilter implements Filter {    private static Log log = LogFactory.getLog(CharacterEncodingFilter.class);    private boolean ignore = true;    private String encoding;    private FilterConfig filterConfig;        @Override    public void init(FilterConfig filterConfig) throws ServletException {        this.filterConfig = filterConfig;        String value = filterConfig.getInitParameter("ignore");                //ignore就是说,忽略掉页面默认编码,统一采用web.xml中的字符集配置,强制转码        if (value == null) {            this.ignore = true;        } else if (value.equalsIgnoreCase("true")) {            this.ignore = true;        } else {            this.ignore = false;        }    }        @Override    public void doFilter(ServletRequest servletRequest,            ServletResponse servletResponse, FilterChain filterChain)            throws IOException, ServletException {        HttpServletRequest request = (HttpServletRequest) servletRequest;        HttpServletResponse response = (HttpServletResponse) servletResponse;        log.info("开始过滤");        encoding = getUserEncoding();        if (ignore && encoding == null) {            encoding=filterConfig.getInitParameter("encoding");        }        if (encoding != null) {            request.setCharacterEncoding(encoding);            response.setContentType("text/html;charset=" + encoding);        }        filterChain.doFilter(request, response);    }        // 用户项目中定义字符集(配置文件中、在session中获取)    public String getUserEncoding() {            return null;    }        @Override    public void destroy() {        }    }


    web.xml


    <!-- 过滤器 -->    <filter>        <filter-name>CharacterEncodingFilter</filter-name>        <filter-class>            com.riambsoft.report.servlet.CharacterEncodingFilter        </filter-class>        <init-param>            <param-name>ignore</param-name>            <param-value>true</param-value>        </init-param>        <init-param>            <param-name>encoding</param-name>            <param-value>GBK</param-value>        </init-param>    </filter>            <filter-mapping>        <filter-name>CharacterEncodingFilter</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>    <!--过滤器结束-->


    注意 : ignore这个参数就是说,忽略掉页面默认编码,统一采用web.xml中的字符集配置,强制转码!!


    这个ignore参数,自己思考了好久好久才发现是这个用途,靠,记忆力果然不可靠,不写注释害人匪浅啊~~


    记住,过滤器的xml配置要优先放于最前边,放在<servlet>之前,才能生效!


    下一篇介绍 servlet过滤器2,对于用户非法在线的处理~~很简单的 http://hi.baidu.com/ae6623/item/617c46c5a96b6dd196445292

  • 相关阅读:
    OSI Model Reference graphic / What do you guys think? Anything to edit/remove/add?
    Iframe Target for a Link
    Abp FullAuditedAggregateRoot
    After Effects上课视频
    Openwrt NAT ALG
    ipcalc.sh
    网络流量测试工具
    NatTypeTester
    基于DPDK开源项目
    GPS的调试与报文解析
  • 原文地址:https://www.cnblogs.com/ae6623/p/4416559.html
Copyright © 2020-2023  润新知