servlet request.setParameters 方法 自创

由于需求需要,,,,像request.setParameters 一样的方法。所以了,,,,,,简单的写了一个实现方法

/**
    * 设置Parameters 的值
    * @param key
    * @param val
    */
    public void setParameters(String key,String val){
    	Map m = getRequest().getParameterMap();
		java.lang.reflect.Field lockedField;
		try {
			lockedField = m.getClass().getDeclaredField("locked");
			lockedField.setAccessible(true);
			//System.out.println(lockedField.get(m));
			lockedField.set(m, false);
			//System.out.println(lockedField.get(m));
			m.put(key, val);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
	
    }

   

	public void doFilter(ServletRequest servletRequest,
			ServletResponse servletResponse, FilterChain arg2)
			throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) servletRequest;
		HttpServletResponse response = (HttpServletResponse) servletResponse;
		//StringBuffer buffer=request.getRequestURL();
		try {
			ParameterRequestWrapper parameterRequestWrapper = new ParameterRequestWrapper(request);
			arg2.doFilter(parameterRequestWrapper, servletResponse);
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
			request.setAttribute("exceptionMsg", e);
			request.getRequestDispatcher(errorPage).forward(request, response);
		}
	}

 

public class ParameterRequestWrapper extends HttpServletRequestWrapper {


	public ParameterRequestWrapper(HttpServletRequest request) {
		super(request);
	}

	public String getParameter(String name) {
		String v = super.getParameter(name);
		if(v == null){
			v = (String)getParameterMap().get(name);
		}
	    return v;
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值