这个请求头数据的获取逻辑不是很懂,也不明白这里的遍历为什么是用**while**,有大佬能解释一下吗?
package com.cherry.requset;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
@WebServlet("/RequestHander")
public class RequestHander extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取所有请求头名称
Enumeration<String> headerNames = request.getHeaderNames();
//2.遍历
while (headerNames.hasMoreElements()){
String name = headerNames.nextElement();
//3.根据名称获取请求头得值
String value = request.getHeader(name);
System.out.println(name+"---"+value);
}
}
}