HttpServletRequest request 获取form参数的两种方式

@RequestMapping(value="/pay",method = RequestMethod.POST)
	public String buildRequest(HttpServletRequest request){
		Map parmMap=new HashMap();
		//方式一:getParameterMap(),获得请求参数map
		Map map= request.getParameterMap();
		//参数名称
		Set key=map.keySet();
		//参数迭代器
		Iterator iterator = key.iterator();
		while(iterator.hasNext()){
			String k=iterator.next();
			parmMap.put(k, map.get(k)[0]);
		}
		System.out.println("parmMap====="+parmMap.toString());
		
		
		//方式二:getParameterNames():获取所有参数名称
		Enumeration a = request.getParameterNames();
		String parm=null;
		String val="";
		while(a.hasMoreElements()){
			//参数名
			parm=a.nextElement();
			//值
			val=request.getParameter(parm);
			parmMap.put(parm, val);
		}
		System.out.println("parmMap=========="+parmMap);
		
		return "";
	}

你可能感兴趣的:(java,web)