1. 获取请求头信息
//获取一个请求头信息
String request.getHeader(String name);
//获取多个请求头信息
Enumeration<String> request.getHeaders(String name); //注意,在HTTP请求里,一个name可以对应多个value。
Enumeration<String> request.getHeaderNames();
int getIntHeader(String name);
long getDateHeader(String name); //日期对应毫秒
2. 重点头信息
1. referer:它可以防止盗链。
2. user-agent: 它可以获取浏览器信息。例如,某网站只在特定浏览器上访问,不是特定浏览器,不让访问。
package com.il.servlet.request;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class UserAgent extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String userAgent = request.getHeader("User-Agent");
if(userAgent.contains("FireFox")) {
System.out.println("火狐浏览器");
}else if(userAgent.contains("Chrome")) {
System.out.println("谷歌浏览器");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}