转自:https://my.oschina.net/sub/blog/182408

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

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

getContextPath获取的是项目的相对路径,例如项目名称为palace,则为/palace

getRequestURI获取的是调用接口的地址,例如/palace/root.getInfo.do

getServletPath为获取的配置到web.xml中的匹配的??

其中 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());
}

 

待看:http://stackoverflow.com/questions/4140448/difference-between-and-in-servlet-mapping-url-pattern

url-pattern详解 
在web.xml文件中,以下语法用于定义映射: 
l. 以”/’开头和以”/*”结尾的是用来做路径映射的。 
2. 以前缀”*.”开头的是用来做扩展映射的。 
3. “/” 是用来定义default servlet映射的。 
4. 剩下的都是用来定义详细映射的。比如: /aa/bb/cc.action 
所以,为什么定义”/*.action”这样一个看起来很正常的匹配会错?因为这个匹配即属于路径映射,也属于扩展映射,导致容器无法判断。

【转】request.getServletPath()和request.getPathInfo()用法的更多相关文章

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

    文章完全转载自 : https://blog.csdn.net/qq_27770257/article/details/79438987 最近对于request中的几种“路径”有点混淆,查找网上资源都 ...

  2. request.getServletPath(),request.getContextPath()

    2018-11-24  16:34:33 1. getServletPath():获取能够与“url-pattern”中匹配的路径,注意是完全匹配的部分,*的部分不包括. 2. getPageInfo ...

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

    1. getServletPath():获取能够与“url-pattern”中匹配的路径,注意是完全匹配的部分,*的部分不包括. 2.getContextPath():获取项目的根路径

  4. request.getRequestURI() 、request.getRequestURL() 、request.getContextPath()、request.getServletPath()区别

    request.getRequestURI() /jqueryWeb/resources/request.jsprequest.getRequestURL() http://localhost:808 ...

  5. request.getRequestURL()和request.getRequestURI()的区别

    request.getRequestURL() 返回全路径 request.getRequestURI() 返回除去host(域名或者ip)部分的路径 request.getContextPath() ...

  6. C#中 Request, Request.params , Request.querystring , Request.Form 区别 与联系用法

    C#中 Request, Request.params , Request.querystring , Request.Form 区别 与联系用法? Request.params , Request ...

  7. [转载]request.getServletPath()方法

    假定你的web application 名称为news,你在浏览器中输入请求路径: http://localhost:8080/news/main/list.jsp 则执行下面向行代码后打印出如下结果 ...

  8. Request、Request.Form、Request.QueryString 用法的区别

    Request.Form:获取以POST方式提交的数据. Request.QueryString:获取地址栏参数(以GET方式提交的数据). Request:包含以上两种方式(优先获取GET方式提交的 ...

  9. ASP.NET中Request.ApplicationPath、Request.FilePath、Request.Path、.Request.MapPath

    1.Request.ApplicationPath->当前应用的目录 2.Request.FilePath->对应于iis的虚拟目录   如 URL http://mockte.com/1 ...

随机推荐

  1. Yii2 高级模板添加更多Application

    单独的前端和后端有时是不够的. 如果需要额外的应用程序,例如博客blog: 1.将frontend复制到blog,环境/ dev / frontend到environments / dev / blo ...

  2. CentOS光盘挂载命令以及安装软件

    最近又学习了一个命令:mount 挂载命令,我们在安装软件的时候,直接敲命令install 包名,但是这里其实是联网安装的, 如果使用光盘,从本地安装就要使用mount命令. 1.我的linux系统是 ...

  3. 整理:Javascript获取数组中的最大值和最小值的方法汇总

    方法一: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 //最小值 Array.prototype.min = function ...

  4. Lr IP欺骗设置

    IP欺骗设置IP工具:IP Wizard 开启IP欺骗时会关闭DHCP(也就是关闭IP自动获取 更改为手动设置IP) 注:添加IP欺骗,和释放IP,都要重启机器后才会生效,IP Wizard要管理员身 ...

  5. C语言 活动安排问题之二

    有若干个活动,第i个开始时间和结束时间是[Si,fi),活动之间不能交叠,要把活动都安排完,至少需要几个教室? #include <stdio.h> #include <string ...

  6. Python: with...as...

    with open(path, 'r') as f: Soup = BeautifulSoup(f.read(), 'lxml') titles = Soup.select('ul > li & ...

  7. 安装VS2015历险记

    最近,因为一些需求,需要在家里的笔记本上安装VS2015.本来以为一件很容易的一件事,发现做起来遇到了很多坑. 首先,下载VS2015,这就花费了两三个小时,安装包太大了.下载完,安装.安装不成功,报 ...

  8. 正则-RegExp()构造函数

    上次写了js正则的字面量声明,今天说说RegExp()构造函数声明: var p=/cat/g;   //字面量声明var p=new RegExp('cat','g') //构造函数声明 它两所表达 ...

  9. 手把手教你玩转nginx负载均衡(二)----安装虚拟机操作系统

    引言 在上一篇,我们组装好了虚拟机的硬件部分,那么现在我们就要把操作系统装上了,既然是服务器,那么安装linux操作系统是个比较好的选择,如果你喜欢的话,安装windows也是没有任何问题的 我这里选 ...

  10. AVL Insight 开源情报工具:一站式情报管理服务

    一.概要 AVL Insight 开源情报工具是安天移动安全推出的一款情报收集工具,它是配合AVL Insight移动威胁情报平台的Chrome浏览器扩展程序,用户可以使用该工具,对网站中的公开信息进 ...