首页  

java web项目中获取重要的请求信息     所属分类 java 浏览量 1203
通过request对象可以获取很多有用的信息

以请求 http://codefun007.xyz/a/article_detail/137.htm 为例

request.getRequestURL() 返回全路径 
/view/article_detail.htm

request.getRequestURI() 返回除去host(域名或者ip)部分的路径
/view/article_detail.htm

request.getContextPath() 返回应用路径,如果应用映射为/,返回为空

request.getServletPath() 返回除去host和应用部分的路径 
/view/article_detail.htm

因为应用直接部署在tomcat ROOT下 应用映射为/
getRequestURI 与 getServletPath 返回的内容一致

request.getQueryString()  返回 id=137

request.getRemoteHost()  获取客户端 请求 ip 

request.getHeader("user-agent") 获取浏览器信息
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36

不同的爬虫会设置不同的 user-agent 信息

Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)
Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)
Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots)
Mozilla/5.0 (compatible; AhrefsBot/5.2; +http://ahrefs.com/robot/)

从网站的后台监控来看 ,还是经常有爬虫过来抓数据的 

request.getSession().getId()   获取sessionID


request.getHeader("Referer")  获取 Referer信息
譬如通过网站首页点击进入  Referer信息就是 http://codefun007.xyz

上一篇     下一篇
memcached知识点整理

A 股 的 真 相

10 Golden rules of doing business in China

redis知识点整理

数据备份三二一原则

蘑菇街5万股期权值多少钱