【HttpServlet】get获取请求信息

Http协议定义了与服务器交互的方式,分别是get,post,delete
使用HttpServlet,在后端接受请求头,请求信息
一、获取请求头

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //取出请求里面的所有头信息
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()){
            String name =(String) headerNames.nextElement();
            String value= request.getHeader(name);
            System.out.println(name=value);
        }、
        System.out.println("1------------------------");
   }

二、获取请求内容

   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //2.获取客户端提交的数据,单个数据
        String name = request.getParameter("name");
        String address = request.getParameter("address");
        System.out.println(name);
        System.out.println("2------------------------");
        //3.获取数据,枚举集合
        //map,key对应多个value,测试name对应多个值
        Map<String,String[]> map = request.getParameterMap();
        Set<String> keyset = map.keySet();
        Iterator<String> iterator = keyset.iterator();
        while (iterator.hasNext()){
            String key = (String) iterator.next();
            String value = map.get(key)[0];
            String value1 = map.get(key)[1];
            System.out.println(key+"="+value+key+"="+value1);

        }

        System.out.println("3------------------------");
    }
http://localhost:8081/fristweb_war_exploded/HttpServlet?name=test&name=123

结果:
【HttpServlet】get获取请求信息_第1张图片

你可能感兴趣的:(【Java】)