转自: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. s:if 判断

    判断 ArrayList size 是否为0 <s:if test="list.size==0"> <s:if> <s:else> </s ...

  2. calendar的一些操作

    一.通过分析日期函数,根据日期进行一系列操作,例如:我们需要知道2个时间段中所有的日期等等. 由于Calendar 类是一个抽象类,因此我们不能通过new来获取该对象的实例.我们可以通过其类方法 ge ...

  3. Spring框架学习(二)

    一.Spring IOC参数值注入 1.注入基本值 1.1)<value></value>元素可以通过字符串指定属性或构造参数的值.容器将字符串从java.lang.Strin ...

  4. linux 软连接和硬链接

    硬链接 ln sourceFile targetFile 硬链接只能给文件创建,不能为目录建立硬链接,硬链接不能跨分区创建,  硬链接会增加inode连接数, 硬链接的文件删除不影响其他文件 课外: ...

  5. selenium web driver 使用JS修改input属性

    selenium获取input时候,发现type=”hidden” 的input无法修改value,经牛人指点,可以使用js修改 首先html源文件如下,设置为text .hidden.submit ...

  6. $compile

    <html ng-app="compile"> <head> <script src="http://apps.bdimg.com/libs ...

  7. oracle打补丁

    oracle 数据库补丁安装(单实例) ------------24006111 注:务必先安装24006111再安装24315821,否则无法进行正常的补丁安装流程.1.关闭数据库监听和数据库实例 ...

  8. 使用getParts()上传多个文件

    <!DOCTYPE html> <html> <head> <title></title> <meta http-equiv=&quo ...

  9. Yii 1开发日记 -- 搜索功能及Checkbox的实现

    用yii 1实现后台的搜索功能,效果如下图: 1.模型中: public function search() { $criteria = new CDbCriteria; //独立高级搜索 if(is ...

  10. ESXI6.0启用 snmp

    1.首先通过ILO登录远程控制桌面然后按F2输入密码后进入 2.开启shell 3.按住 Alt + F1 切换到shell 4.启用 snmp   esxcli system snmp set -- ...