1)取得请求头相关的信息
*a)request.getHeader(String):String
b)request.getHeaders(String):Enumeration
c)getHeaderNames():Enumeration
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Rdome2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("utf-8");
//客户端接收的语言类型
String acceptLanguage = response.getHeader("Accept-Language");
//客户端版本号的名字
String userAgent = request.getHeader("User-Agent");
//连接的目标主机和端口号
String host = request.getHeader("Host");
response.getWriter().write("客户端接收的语言类型 Accept-Language=" + acceptLanguage + "<br/>");
response.getWriter().write("客户端版本号的名字 User-Agent=" + userAgent + "<br/>");
response.getWriter().write("连接的目标主机和端口号 Host=" + host + "<br/>");
response.getWriter().write("========================================= "+ "<br/>");
//一次性取得所有的请求头信息
Enumeration<String> enums = request.getHeaderNames();
while(enums.hasMoreElements())
{
//请求头
String keys = enums.nextElement();
Enumeration<String> enums2 = request.getHeaders(keys);
while(enums2.hasMoreElements())
{
String value = enums2.nextElement();
response.getWriter().write(keys+"<->"+value+"<br/>");
}
}
}
}
原文地址:http://blog.51cto.com/357712148/2104758