代码演示:
//获取请求行数据 Enumeration names = req.getHeaderNames(); while(names.hasMoreElements()) { String name = (String)names.nextElement(); String value = req.getHeader(name); System.out.println(name+":"+value); }
结果:
host:localhost connection:keep-alive upgrade-insecure-requests:1 user-agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3741.400 QQBrowser/10.5.3863.400 accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8 referer:http://localhost/Web1/method.jsp accept-encoding:gzip, deflate, br accept-language:zh-CN,zh;q=0.9 cookie:JSESSIONID=642977B5CFD81FBB5AF4D9EBB6F8C5DB
注:键名(name)指的是html标签中name属性的值。value即value的值。
注意:
如果要获取的数据不存在,不会报错,返回null
原文:https://www.cnblogs.com/Scorpicat/p/12342310.html