美文网首页
Filter高级开发-增强request

Filter高级开发-增强request

作者: 小小蒜头 | 来源:发表于2017-10-31 18:39 被阅读35次

    由于开发人员在Filter中可以得到代表用户请求和响应的request、response对象,因此在编程中可以使用Decorator(装饰器)模式对request、response对象进行包装,再把包装对象传给目标资源,从而实现一些特殊需求。

    过滤器:CharacterEncodingFilter2.java

    package cn.itcast.filter;
    
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    
    /**
     * Created by yvettee on 2017/10/31.
     */
    //真正解决全站乱码问题
    public class CharacterEncodingFilter2 implements Filter {
        private FilterConfig config;
        private String defaultConfig = "UTF-8";//定义一个缺省的字符集
    
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            this.config = filterConfig;
        }
    
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            String charset = this.config.getInitParameter("charset");
            if (charset == null) {
                charset = defaultConfig;
            }
    
            HttpServletRequest request = (HttpServletRequest) servletRequest;
            HttpServletResponse response = (HttpServletResponse) servletResponse;
    
            request.setCharacterEncoding(charset);
            response.setCharacterEncoding(charset);
            response.setContentType("text/html;charset = " + charset);
    
            filterChain.doFilter(new MyRequest(request), response);
        }
    
        /*
        1.写一个类,实现与被增强对象相同的接口
        2.定义一个变量,记住被增强对象
        3.定义一个构造方法,接收被增强对象
        4.覆盖想增强的方法
        5.对于不想增强的方法,直接调用被增强对象(目标对象)的方法
         */
    
        //编写自定义类
        class MyRequest extends HttpServletRequestWrapper {
    
            private HttpServletRequest request;
    
            public MyRequest(HttpServletRequest request) {
                //传一个被增强对象request进去
                super(request);
                this.request = request;
            }
    
            // 覆盖需要增强的getParameter方法
            @Override
            public String getParameter(String name) {
                String value = request.getParameter(name);
                if (!request.getMethod().equalsIgnoreCase("get")) {
                    return value;
                }
                if (value == null) {
                    return null;
                }
                try {
                    return value = new String(value.getBytes("iso8859-1"), request.getCharacterEncoding());
                } catch (UnsupportedEncodingException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    
        @Override
        public void destroy() {
    
        }
    }
    
    

    web.xml

     <filter>
            <filter-name>CharacterEncodingFilter2</filter-name>
            <filter-class>cn.itcast.filter.CharacterEncodingFilter2</filter-class>
            <init-param>
                <param-name>charset</param-name>
                <param-value>UTF-8</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>CharacterEncodingFilter2</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    

    源代码:https://github.com/yvettee36/FilterBase
    上篇:Filter案例四—实现用户自动登录的过滤器
    下篇:转义Filter

    相关文章

      网友评论

          本文标题:Filter高级开发-增强request

          本文链接:https://www.haomeiwen.com/subject/cvpbpxtx.html