获取请求头数据
* 方法:
* String getHeader(String name):通过请求头的名称获取请求头的值
* Enumeration
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("/requestDemo2")
public class RequestDemo2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取请求体
//获取所有请求头的名称
Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String s = headerNames.nextElement();
//获取请求头名称的值
String header = request.getHeader(s);
System.out.println(s+ " : " +header);
}
}
}
原文:https://www.cnblogs.com/codegzy/p/14743074.html