为什么80%的码农都做不了架构师?>>>   

在 Web 中,我们通常需要获取 URL 相对于 Webapp 的路径,主要是下面的几个方法:

request.getServletPath()
request.getPathInfo()
request.getContextPath()
request.getRequestURI()

其中 request.getRequestURI() 的返回值包含了 request.getContextPath(),所以是相对于网站的根目录的。

下面我们分析 request.getServletPath() 和 request.getPathInfo()

1. 如果我们的 servlet-mapping 如下配置:

<servlet-mapping><servlet-name>jetbrick-template</servlet-name><url-pattern>*.jetx</url-pattern>
</servlet-mapping>

那么访问: /context/templates/index.jetx

request.getServletPath() == "/templates/index.jetx"
request.getPathInfo() == <null>

2. 如果我们的 servlet-mapping 如下配置:

<servlet-mapping><servlet-name>jetbrick-template</servlet-name><url-pattern>/*</url-pattern>
</servlet-mapping>

那么访问: /context/templates/index.jetx

request.getServletPath() == ""
request.getPathInfo() == "/templates/index.jetx"

3. 如果我们的 servlet-mapping 如下配置:

<servlet-mapping><servlet-name>jetbrick-template</servlet-name><url-pattern>/template/*</url-pattern>
</servlet-mapping>

那么访问: /context/templates/index.jetx

request.getServletPath() == "/templates"
request.getPathInfo() == "/index.jetx"

总结

所以,我们要获取相对于 request.getContextPath() 的路径,我们可以使用如下的代码:

String uri = request.getServletPath();
String pathInfo = request.getPathInfo();
if (pathInfo != null && pathInfo.length() > 0) {uri = uri + pathInfo;
}

或者:

String uri = request.getRequestURI();
String contextPath = request.getContextPath();
if (contextPath != null && contextPath.length() > 0) {uri = uri.substring(contextPath.length());
}

===============================

2013-02-08 补上一个 Tomcat 自身的实现:

catalina.jar,  DefaultServlet.java

protected String getRelativePath(HttpServletRequest request){if (request.getAttribute("javax.servlet.include.request_uri") != null){String result = (String)request.getAttribute("javax.servlet.include.path_info");if (result == null) {result = (String)request.getAttribute("javax.servlet.include.servlet_path");} else {result = (String)request.getAttribute("javax.servlet.include.servlet_path") + result;}if ((result == null) || (result.equals(""))) {result = "/";}return result;}String result = request.getPathInfo();if (result == null) {result = request.getServletPath();} else {result = request.getServletPath() + result;}if ((result == null) || (result.equals(""))) {result = "/";}return result;}

转载于:https://my.oschina.net/sub/blog/182408

request.getServletPath()和request.getPathInfo()用法相关推荐

  1. 关于request.getServletPath(),request.getContextPath()的总结

    最近对于request中的几种"路径"有点混淆,查找网上资源都没有很好的总结,希望此文章能够帮助我理解一下这几种"路径". ++++++++++++++++++ ...

  2. java getpathinfo_request.getServletPath()和request.getPathInfo()用法

    在 Web 中,我们通常需要获取 URL 相对于 Webapp 的路径,主要是下面的几个方法: request.getServletPath() request.getPathInfo() reque ...

  3. request.getRequestURL()和request.getRequestURI()区别

    request.getRequestURL() 返回全路径 request.getRequestURI() 返回除去host(域名或者ip)及端口号部分的路径 request.getContextPa ...

  4. request的setAttribute()用法及request.sendRedirect 与 request.getRequestDispatcher.forward 的区别

    request.setAttribute()怎么用的? JSP1代码 String [] test=new String[2]; test[0]="1"; test[1]=&quo ...

  5. JSPServlet中request.getParameter() 和request.getAttribute() 区别

    一.request.getParameter() 和request.getAttribute() 区别 (1)request.getParameter()取得是通过容器的实现来取得通过类似post,g ...

  6. python request microsoft graph_Python request.headers方法代码示例

    本文整理汇总了Python中flask.request.headers方法的典型用法代码示例.如果您正苦于以下问题:Python request.headers方法的具体用法?Python reque ...

  7. Request、Request.Form和Request.QueryString的区别

    Request.Request.Form和Request.QueryString的区别 Request.Form:获取以POST方式提交的数据(接收Form提交来的数据): Request.Query ...

  8. django之视图系统 views.py--主要内容(FBV和CBV、dispath、request对象和request.FILES、JsonResponse)...

    django之视图系统 views.py-->主要内容(FBV和CBV.dispath.request对象和request.FILES.JsonResponse) 一个视图函数(类),简称视图, ...

  9. request.getParameter和request.getAttribute之间的区别

    HttpServletRequest类既有getAttribute()方法,也由getParameter()方法,这两个方法有以下区别: (1)HttpServletRequest类有setAttri ...

最新文章

  1. Spring全局异常处理
  2. QT中实现QLineEdit变成圆角以及QCombox大半空白,小部分下拉框样式
  3. SDNU 1217 CD收藏——并查集
  4. 《鸟哥的Linux私房菜--基础篇》学习
  5. 如何查看 Linux 中所有正在运行的服务
  6. Faster R-CNN源码中RPN的解析(自用)
  7. 鼠标悬停显示图片html5,JavaScript 鼠标悬停图片,显示隐藏文本
  8. Linux 引导流程解析
  9. 数据挖掘导论课后习题答案-第三章
  10. 查看服务器虚拟机版本,查看虚拟机版本命令
  11. 关于工程总承包(EPC)项目最高限价的说明
  12. java 读取word模板文件路径_Java 读取Word模板替换内容并另存
  13. html5 案例练习(注册页面)
  14. 华为ModelArts笔记1
  15. 2023,数字政务潮水已至
  16. 让人发狂的sql语句!
  17. python 读取gmail 邮箱消息
  18. SEO笔记--代码优化(三)
  19. 计算机组成TEC4,计算机组成原理实验系统TEC4详细资料
  20. ❤️AI从入门到到精通❤️

热门文章

  1. CCF C³:创业公司如何面对开源?知乎CTO李大海:谢邀
  2. 小马智行L4无人车今日量产下线,这比融资更具里程碑意义
  3. 百度股价接连暴涨的背后,看Apollo的2020
  4. 用上AI后,银行每年竟然能多赚1万亿美元丨麦肯锡最新调查报告
  5. Nature:首个完全复现人眼的仿生眼问世,港科大造出半球形人工视网膜,感光性能超过人眼460倍...
  6. 中国首份AI落地白皮书发布!地方政府规模大,金融领域最积极,北京供给超上海深圳总和...
  7. 禾多推出高精度定位和自动驾驶验证平台!倪凯:助力行业量产,定位中国向导...
  8. 蚂蚁财富联手百会CRM全面升级金融服务
  9. Java调用net的webservice问题分享
  10. 利用circpedia 数据库探究circRNA的可变剪切