URL、URI、ServletPath、ContextPath示例

获取各种请求地址代码:

String contextPath = request.getContextPath();
String servletPath = request.getServletPath();
String pathInfo = request.getPathInfo();
String requestURI = request.getRequestURI();
String requestURL = request.getRequestURL().toString();

结果示例:

以下测试均在contextPath(项目名)为demo,请求路径@GetMapping/hello/*条件下进行

  1. 请求地址:http://localhost:8080/demo/hello/abcDispatcherServleturl-patternspring.mvc.servlet.path=/
contextPath: /demo
requestURI: /demo/hello/abc
servletPath: /hello/abc
pathInfo: null
requestURL: http://localhost:8080/demo/hello/abc
  1. 请求地址:http://localhost:8080/demo/hello/hello/abcDispatcherServleturl-patternspring.mvc.servlet.path=/hello
contextPath: /demo
requestURI: /demo/hello/hello/abc
servletPath: /hello
pathInfo: /hello/abc
requestURL: http://localhost:8080/demo/hello/hello/abc

结论:

servletPathpathInfo的结果与DispatcherServleturl-pattern配置有关,servletPath + pathInfo组成不含项目名的请求路径(以下符号-为减号

  1. url-pattern/
    servletPathrequestURI - 项目名
    pathInfonull
  2. url-pattern/other时(这里的other表示其他任意路径)
    servletPath/other
    pathInforequestURI - 项目名 - servletPath
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值