javaweb-servlet-params方法的几种案例

(1)
package www.csdn.net.servlet.params;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ParamServletDemo1 extends HttpServlet {

	/**
		doGet方法
	 */

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

//		根据请求的参数名称获取请求的参数名称对应的值
		System.out.println(request.getParameter("name"));
		
	}

	/**
		doPost的方法
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
(2)
package www.csdn.net.servlet.params;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ParamServletDemo2 extends HttpServlet {

	/**
		doGet的方法
	 */

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		//根据参数名 获取其参数名对应的多个值
		String values[] = request.getParameterValues("name");
		for(String value:values){
			System.out.println(value);
		}
	}

	/**
		doPos的方法
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
(3)
package www.csdn.net.servlet.params;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ParamServletDemo3 extends HttpServlet {

	/**
		doGet方法
	 */

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
//获取的是请求参数的名称集合
		Enumeration e=request.getParameterNames();
//	遍历
		while(e.hasMoreElements()){
//			获取具体的请求参数名称
			String name=e.nextElement();
//			获取参数名称对应的值
			String values[]=request.getParameterValues(name);
//			遍历
			for(String value:values){
				System.out.println(name+":"+value);
			}
		}
	}

	/**
		doPost的方法
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
(4)
package www.csdn.net.servlet.params;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ParamServletDemo4 extends HttpServlet {

	/**
		doGet的方法
	 */

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

//		String对应的是参数名称  String[]对应的是参数名称对应值
	Map map=request.getParameterMap();
//	遍历Map集合
//	首先获取map集合中key键对应的set集合
	Set keys=map.keySet();
//	得到key对应的迭代器
	Iterator it=keys.iterator();
//	遍历迭代器
while(it.hasNext()){
//	获取具体的key
	String key=it.next();
//	根据map的get(key)方法获取key对应的value值
	String values[]=map.get(key);
	System.out.println("参数名称:"+key+":");
//	遍历数组
	for(String value:values){
		System.out.println(value+",");
	}
	System.out.println("------------");
}
//		获取map集合的实体对象
		Set> set=map.entrySet();
//	获取实体set集合对象的迭代器
		Iterator> it1=set.iterator();
	while(it1.hasNext()){
		Entry entry=it1.next();
		System.out.println("参数名称"+entry.getKey());
		String values[]=entry.getValue();
//		遍历数组
		for(String value:values){
			System.out.println(value+",");
			
		}
		System.out.println("==============");
	
	
	}
	}

	/**
		doPost的方法
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}




你可能感兴趣的:(javaEE)