HttpServletRequest request 方法记录

今天在使用request中的一些方法时:对request中的方法还不明确,特此记录一下。

常用获取url参数的方法:参考地址https://www.cnblogs.com/panxuejun/p/7743436.html

1、request.getRequestURL()

返回的是完整的url,包括Http协议,端口号,servlet名字和映射路径,但它不包含请求参数。
2、request.getRequestURI()

得到的是request URL的部分值,并且web容器没有decode过的

3、request.getContextPath() 

返回对应springboot中 tomcat 配置的 server.context-path

4、request.getServletPath() 
返回调用servlet的部分url. 对应@requestmapping 配置

5、request.getQueryString() 
返回url路径后面的查询字符串

以下为实际请求结果情况:

post:http://127.0.0.1:19000/api/filesys/requestTest?url=https://image.baidu.com/search/detail?ct=503316480&z=0&ipn=d&word=百度&step_word=&hs=0&pn=1&spn=0&di=26290&pi=0&rn=1&tn=baiduimagedetail&is=0%2C0&istype=0&ie=utf-8&oe=utf-8&in=&cl=2&lm=-1&st=undefined&cs=1259821177%2C3195294575&os=2658457481%2C3885594412&simid=4173905492%2C658294463&adpicid=0&lpn=0&ln=1488&fr=&fmq=1573028929370_R&fm=&ic=undefined&s=undefined&hd=undefined&latest=undefined©right=undefined&se=&sme=&tab=0&width=undefined&height=undefined&face=undefined&ist=&jit=&cg=&bdtype=0&oriquery=&objurl=http%3A%2F%2Fa.36krcnd.com%2Fphoto%2F2014%2F5be404781da19da8778d6cbf0296acc1.jpg&fromurl=ippr_z2C%24qAzdH3FAzdH3Fnmh6_z%26e3Bv54AzdH3FrAzdH3Fd8cc90_z%26e3Bip4s&gsm=&rpstart=0&rpnum=0&islist=&querylist=&force=undefined

 

1. request.getQueryString();

结果: url=https://image.baidu.com/search/detail?ct=503316480&z=0&ipn=d&word=%E7%99%BE%E5%BA%A6&step_word=&hs=0&pn=1&spn=0&di=26290&pi=0&rn=1&tn=baiduimagedetail&is=0%2C0&istype=0&ie=utf-8&oe=utf-8&in=&cl=2&lm=-1&st=undefined&cs=1259821177%2C3195294575&os=2658457481%2C3885594412&simid=4173905492%2C658294463&adpicid=0&lpn=0&ln=1488&fr=&fmq=1573028929370_R&fm=&ic=undefined&s=undefined&hd=undefined&latest=undefined©right=undefined&se=&sme=&tab=0&width=undefined&height=undefined&face=undefined&ist=&jit=&cg=&bdtype=0&oriquery=&objurl=http%3A%2F%2Fa.36krcnd.com%2Fphoto%2F2014%2F5be404781da19da8778d6cbf0296acc1.jpg&fromurl=ippr_z2C%24qAzdH3FAzdH3Fnmh6_z%26e3Bv54AzdH3FrAzdH3Fd8cc90_z%26e3Bip4s&gsm=&rpstart=0&rpnum=0&islist=&querylist=&force=undefined

2. request.getContentType();

结果: null

3. request.getRequestURI();

结果: /api/filesys/requestTest

4. request.getRequestURL();

结果: http://127.0.0.1:19000/api/filesys/requestTest

5. request.getServletPath();

结果: /requestTest

6. request.getContextPath();

结果: /api/filesys

7. request.getScheme();

结果: http

8. request.getServerPort();

结果: 19000

9. request.getServerName();

结果: 127.0.0.1

10. request.getParameter("url");

结果: https://image.baidu.com/search/detail?ct=503316480

注意

这里request 获取方法的结果不一定会实际地址所一致,是因为request方法受 请求headers 中host 和origin的配置影响 所以导致请求路径和实际会有所不同

特别注意的是从上述例子可以看出,getParameter 获取的参数是以&分割的,会受&影响

你可能感兴趣的:(每日随笔)