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

更改Request Parameters中的值

时间:2015-02-10 12:57:54      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

1. 定义ParameterRequestWrapper 继承HttpServletRequestWrapper

public class ParameterRequestWrapper extends HttpServletRequestWrapper {

    private Map<String,String[]> params;

    public ParameterRequestWrapper(HttpServletRequest request, Map<String,String[]> newParams) {
        super(request);
        this.params = newParams;
    }

    @Override
    public String getParameter(String name) {
        Object v = params.get(name);
        if (v == null) {
            return null;
        } else if (v instanceof String[]) {
            String[] strArr = (String[]) v;
            if (strArr.length > 0) {
                return strArr[0];
            } else {
                return null;
            }
        } else if (v instanceof String) {
            return (String) v;
        } else {
            return v.toString();
        }
    }

    @Override
    public Map getParameterMap() {
        return params;
    }

    @Override
    public Enumeration getParameterNames() {
        return new Vector(params.keySet()).elements();
    }

    @Override
    public String[] getParameterValues(String name) {
        Object v = params.get(name);
        if (v == null) {
            return null;
        } else if (v instanceof String[]) {
            return (String[]) v;
        } else if (v instanceof String) {
            return new String[] { (String) v };
        } else {
            return new String[] { v.toString() };
        }
    }

}

2. 在filter 中编写如下代码:

public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        Map<String, String[]> parametersMap = DataFormat
                .formatRequestParameters(httpRequest);
        ParameterRequestWrapper requestWrapper = new ParameterRequestWrapper(
                httpRequest, parametersMap);
        chain.doFilter(requestWrapper, response);
    }

更改Request Parameters中的值

标签:

原文地址:http://www.cnblogs.com/tangyanbo/p/4283454.html

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