request应用-请求头学习笔记
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了request应用-请求头学习笔记相关的知识,希望对你有一定的参考价值。
request对象常用API1)取得请求头相关的信息
*a)request.getHeader(String):String
b)request.getHeaders(String):Enumeration
c)getHeaderNames():Enumeration
java代码实现:
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/>");
}
}
}
}
以上是关于request应用-请求头学习笔记的主要内容,如果未能解决你的问题,请参考以下文章