Filter中修改Request的方法

最近做项目有需求需要在filter中修改请求的参数,找了半天终于找到一些,不是新东西,在其他地方也找得到,在这记录整理一下,方便以后查找

首先我们要重新封装request参数,我们继承HttpServletRequestWrapper

//package信息
 
import java.util.Enumeration;
import java.util.Map;
import java.util.Vector;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
class ParameterRequestWrapper extends HttpServletRequestWrapper {
 
    private Map<String, String[]> params;  //参数map
 
    public ParameterRequestWrapper(HttpServletRequest request,
            Map<String, String[]> newParams) {
        super(request);
         
        this.params = newParams;
 
        // RequestDispatcher.forward parameter
        renewParameterMap(request);
    }
 
    @Override
    public String getParameter(String name) {
        String result = "";
         
        Object v = params.get(name);
        if (v == null) {
            result = null;
        } else if (v instanceof String[]) {
            String[] strArr = (String[]) v;
            if (strArr.length > 0) {
                result =  strArr[0];
            } else {
                result = null;
            }
        } else if (v instanceof String) {
            result = (String) v;
        } else {
            result =  v.toString();
        }
         
        return result;
    }
 
    @Override
    public Map<String, String[]> getParameterMap() {
        return params;
    }
 
    @Override
    public Enumeration<String> getParameterNames() {
        return new Vector<String>(params.keySet()).elements();
    }
 
    @Override
    public String[] getParameterValues(String name) {
        String[] result = null;
         
        Object v = params.get(name);
        if (v == null) {
            result =  null;
        } else if (v instanceof String[]) {
            result =  (String[]) v;
        } else if (v instanceof String) {
            result =  new String[] { (String) v };
        } else {
            result =  new String[] { v.toString() };
        }
         
        return result;
    }
 
    private void renewParameterMap(HttpServletRequest req) {
 
        String queryString = req.getQueryString();
 
        if (queryString != null && queryString.trim().length() > 0) {
            String[] params = queryString.split("&");
 
            for (int i = 0; i < params.length; i++) {
                int splitIndex = params[i].indexOf("=");
                if (splitIndex == -1) {
                    continue;
                }
                 
                String key = params[i].substring(0, splitIndex);
 
                if (!this.params.containsKey(key)) {
                    if (splitIndex < params[i].length()) {
                        String value = params[i].substring(splitIndex + 1);
                        this.params.put(key, new String[] { value });
                    }
                }
            }
        }
    }
 
}

然后是一个例子filter看看如何写

package ;
 
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;
 
public class ParamFilter implements Filter{
 
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // TODO Auto-generated method stub
         
    }
 
    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        Map<String,String[]> m = new HashMap<String,String[]>(request.getParameterMap()); //首先取出参数map
     //此处转换 这里面参数都是以String类型数组出现的,所以替换的时候需要注意也替换成String数组~~
request = new ParameterRequestWrapper((HttpServletRequest)request, m); //重新封装一个request chain.doFilter(request, response); } @Override public void destroy() { // TODO Auto-generated method stub } }

 

posted @ 2015-07-23 08:32  DeanChiong  阅读(886)  评论(0编辑  收藏  举报