自学Servlet_7_resquest(获取客户机带过来的数据)

获取客户机带过来的数据三种情况:
//1.
		//http://localhost:8080/day06/servlet/RequestDemo3?name=xxx
		String value = request.getParameter("name");
		System.out.println(value);
		
		System.out.println("-----");
		
		
		//2.
		//http://localhost:8080/day06/servlet/RequestDemo3?name=xxx&password=123
		Enumeration e = request.getParameterNames();
		while(e.hasMoreElements()){
			String name = (String) e.nextElement();
			value = request.getParameter(name);
			System.out.println(name + "=" + value );
		}
		
		System.out.println("----");
		
		//3.
		//http://localhost:8080/day06/servlet/RequestDemo3?name=xxx&name=yyyy
		String values[] = request.getParameterValues("name");
		/*if(values!=null){
			for(String value1 : values){
				System.out.println(value1);
			}
		}*/
		for(int i=0;values!=null && i<values.length;i++){   //这样的代码可以预防null指针的问题
			System.out.println(values[i]);
		}
		
		
		System.out.println("--map--");
		//4.
		// http://localhost:8080/day06/servlet/RequestDemo3?a=1&a=2&b=1
		Map<String,String[]> map = request.getParameterMap();  //a=1&a=2&b=1
		for(Map.Entry<String, String[]> entry : map.entrySet()){
			String name  = entry.getKey();
			values = entry.getValue();   //[]
			for(int i=0;values!=null && i<values.length;i++){
				value = values[i];
				System.out.println(name + "=" + value);
			}
		}

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