码迷,mamicode.com
首页 > 其他好文 > 详细

request应用-请求头学习笔记

时间:2018-04-18 11:45:33      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:request应用-请求头学习笔记

request对象常用API

1)取得请求头相关的信息
*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应用-请求头学习笔记

标签:request应用-请求头学习笔记

原文地址:http://blog.51cto.com/357712148/2104758

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!