Java 修改请求参数

实际开发中,有时需要在过滤器中修改request请求的参数,但是,request对象只提供了getParameter(String name)方法,并没有提供setParamter方法。

 

或者,走点弯路,先取得参数map( request.getParameterMap()),然后通过map的put方法,将参数放进去,实践证明,该路也是不通的,因为getParameterMap()返回的map是不可更改的,通过put方法往里放数据,会抛异常“java.lang.IllegalStateException: No modifications are allowed to a locked ParameterMap”

 

并且getParameterMap()方法的声明上已经给出说明:

注:immutable 不可改变的
Java 修改请求参数
 

怎么办呢?方法总是有的,自己实现一个HttpServletRequestWrapper,想怎么用就怎么用,完全自主可控。

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

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

public class AMHttpServletRequestWrapper extends HttpServletRequestWrapper{

	Map<String, String[]> params = null;
	
	public AMHttpServletRequestWrapper(HttpServletRequest request,Map inParam) {
		super(request);
		params = new HashMap(inParam);
	}
	
	public void setParameter(String key,String value){
		params.put(key, new String[]{value});
	}
	public void setParameter(String key,String[] values){
		params.put(key, values);
	}

	@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 {  
	            return v.toString();  
	        }  
	}

	@Override
	public Map<String, String[]> getParameterMap() {
		return params;
	}

	@Override
	public Enumeration<String> getParameterNames() {
		 Vector l = new Vector(params.keySet());  
	     return l.elements();  
	}

	@Override
	public String[] getParameterValues(String name) {
		return params.get(name);
	}

}

 上面的AMHttpServletRequestWrapper中,新提供了2个方法:

public void setParameter(String key,String value){
		params.put(key, new String[]{value});
	}
	public void setParameter(String key,String[] values){
		params.put(key, values);
	}

 在java代码中,可以通过这两个方法随意修改request请求的参数了。

 

 

在最前端放置一个过滤器,将原生的request包装成AMHttpServletRequestWrapper,然后继续往后传递,后面的程序拿到的request就是自己的AMHttpServletRequestWrapper了。

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 org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class AMFilter implements Filter {
	
	private static Log log = LogFactory.getLog(AMFilter.class);
	
	@Override
	public void doFilter(ServletRequest req, ServletResponse res,FilterChain chain) throws IOException, ServletException {
		HttpServletRequest http_req = (HttpServletRequest)req;
		AMHttpServletRequestWrapper amHttpServletRequestWrapper = new AMHttpServletRequestWrapper(http_req, req.getParameterMap());
		chain.doFilter(amHttpServletRequestWrapper, res);
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
	}

	@Override
	public void destroy() {
	}

}

 

 

 

你可能感兴趣的:(Java 修改请求参数)