servlet Request接口(2)

package web;

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.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns="/register")
public class RegisterServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
	
		Map<String,String[]> map=req.getParameterMap();
		//public java.util.Map getParameterMap()返回一个将请求消息中的所有参数名和值的Map对象
		//key是参数名(String),value是所对应的字符串值数组
		Set set=map.keySet();
		Iterator it=set.iterator();
		while(it.hasNext()){
			Object[]ob=map.get(it.next());
			for(Object o:ob){
				System.out.println(o);
			}
		}
		System.out.println("--------------");
		
		//map.entrySet()得到的是整个键值对对象
		for(Entry<String,String[]> en:map.entrySet()){
			for(String o:en.getValue()){
				System.out.println(o);
			}
		}
		System.out.println("================");
		
		System.out.println(req.getContextPath());
		System.out.println(req.getRemoteAddr());
		System.out.println(req.getHeader("Referer"));
	}
}


你可能感兴趣的:(servlet Request接口(2))