利用过滤器修改request中的值

1、构建request扩展类

package com.yuchengtech.emp.dataquality.filter;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;


public class RequestWrapper extends HttpServletRequestWrapper {
    private Map params = new HashMap();
    public RequestWrapper(HttpServletRequest request) {
        super(request);
        //将现有parameter传递给params
        this.params.putAll(request.getParameterMap());
    }
 
    /**
     * 重载构造函数
     * @param request
     * @param extraParams
     */
    public RequestWrapper(HttpServletRequest request, Map extraParams) {
        this(request);
        addParameters(extraParams);
    }
 
    public void addParameters(Map extraParams) {
        for (Map.Entry entry : extraParams.entrySet()) {
            addParameter(entry.getKey(), entry.getValue());
        }
    }
 
    /**
     * 重写getParameter,代表参数从当前类中的map获取
     * @param name
     * @return
     */
    @Override
    public String getParameter(String name) {
        String[]values = params.get(name);
        if(values == null || values.length == 0) {
            return null;
        }
        return values[0];
    }
 
    /**
     * 同上
     * @param name
     * @return
     */
    @Override
    public String[] getParameterValues(String name) {
        return params.get(name);
    }
 
    /**
     * 添加参数
     * @param name
     * @param value
     */
    public void addParameter(String name, Object value) {
        if (value != null) {
            if (value instanceof String[]) {
                params.put(name, (String[]) value);
            } else if (value instanceof String) {
                params.put(name, new String[]{(String) value});
            } else {
                params.put(name, new String[]{String.valueOf(value)});
            }
        }
    }
 
}

2、创建过滤器,修改request中的值

package com.yuchengtech.emp.dataquality.filter;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yuchengtech.emp.utils.FilterUtil;

public class EncodeHtmlFilter implements Filter {
    public static final Logger log = LoggerFactory.getLogger(EncodeHtmlFilter.class);

    public void init(FilterConfig filterConfig) throws ServletException {
        log.info("filter init");
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        log.info("doFilter begin");
        Map map = request.getParameterMap();
        Map mapNew = new HashMap();
        if (map != null) {
            for (Map.Entry entry : map.entrySet()) {
                String[] arr = entry.getValue();
                if (arr.length > 0 && arr != null) {
                    arr[0] = FilterUtil.encodeHtml(arr[0].toString());
                }
                mapNew.put(entry.getKey(), arr);
            }
        }
        RequestWrapper requestParameterWrapper = new RequestWrapper((HttpServletRequest)request);
        requestParameterWrapper.addParameters(mapNew);
        chain.doFilter(requestParameterWrapper, response);
        log.info("doFilter end");
    }

    public void destroy() {
        log.info("filter destroy");
    }
}

3、配置web.xml


    encodeHtmlFilter
    
            com.yuchengtech.emp.dataquality.filter.EncodeHtmlFilter
    


    encodeHtmlFilter
    /*

你可能感兴趣的:(利用过滤器修改request中的值)