1. 程式人生 > >HttpServletRequest request 獲取form引數的兩種方式

HttpServletRequest request 獲取form引數的兩種方式

@RequestMapping(value="/pay",method = RequestMethod.POST)
	public String buildRequest(HttpServletRequest request){
		Map<String,String> parmMap=new HashMap<String,String>();
		//方式一:getParameterMap(),獲得請求引數map
		Map<String,String[]> map= request.getParameterMap();
		//引數名稱
		Set<String> key=map.keySet();
		//引數迭代器
		Iterator<String> 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<String> 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 "";
	}