getContextPath、getServletPath、getRequestURI
在浏览器中输入请求路径:
http://localhost:9084/manager/a/a
server:
port: 9084
servlet:
context-path: /manager
@PostMapping(value = "/a/a")
public void queryUserLabel(HttpServletRequest req) {
System.out.println("servletPath:"+req.getServletPath());
System.out.println("contextPath:"+req.getContextPath());
System.out.println("contextPath2:"+req.getServletContext().getContextPath());
System.out.println("uri:"+req.getRequestURI());
System.out.println("url:"+req.getRequestURL());
System.out.println("realPath:"+req.getServletContext().getRealPath("/"));
System.out.println("Protocol:"+req.getProtocol());
System.out.println("ServerName:"+req.getServerName());
System.out.println("LocalName:"+req.getLocalName());
}
servletPath:/a/a
contextPath:/manager
contextPath2:/manager
uri:/manager/a/a
url:http://localhost:9084/manager/a/a
realPath:C:\Users\shi\AppData\Local\Temp\tomcat-docbase.499203428717278740.9084\
Protocol:HTTP/1.1
ServerName:localhost
LocalName:0:0:0:0:0:0:0:1
通过观察打印结果,我们可以总结:
getServletPath():获取项目的根路径后的接口路径
getContextPath():获取项目的根路径
getRequestURI:获取根路径到地址结尾
getRequestURL:获取请求的地址链接(浏览器中输入的地址)
getServletContext().getRealPath("/"):获取“/”在机器中的实际地址
getScheme():获取的是使用的协议(http 或https)
getProtocol():获取的是协议的名称(HTTP/1.11)
getServerName():获取的是域名(xxx.com)
getLocalName:获取到的是IP