转自: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. C和指针 第十五章 习题

    15.8 十六进制倾印码 #include <stdio.h> #include <stdlib.h> #include <string.h> #include & ...

  2. MyBatis处理一行数据-MyBatis使用sum语句报错-MyBatis字段映射-遁地龙卷风

    第二版 (-1)写在前面 我用的是MyBatis 3.2.4 (0) 编程轶事 select sum(value) ,sum(value2)  from integral_list where  Me ...

  3. 提交按钮ajax方式

    function doSubmit2(){ $("#formobj").form("submit", { url : "sjsCorporationC ...

  4. MVC4中基于bootstrap和HTML5的图片上传Jquery自定义控件

    场景:mvc4中上传图片,批量上传,上传前浏览,操作.图片进度条. 解决:自定义jquery控件 没有解决:非图片上传时,会有浏览样式的问题; 解决方案; 1.样式 – bootstrap 的css和 ...

  5. C#和.net的版本

    C#1.0:OOP和CTS(delegate)C#2.0:泛型.分部类.静态类.迭代器.匿名方法.委托的协变和逆变.属性访问器可以被单独设置访问级别.可空类型.??操作符C# 3.0:Linq.lam ...

  6. go:结构体的可访问性

    1.要使某个符号对其他包( package)可见(即可以访问),需要将该符号定义为以大写字母开头------摘自go相关书籍2.go只限制包内外的可访问性,而不限制同包内不同文件的可访问性 本文讨论结 ...

  7. 如何搞定IE+google双内核的360浏览器表单自动回填兼容问题

    最近开发中碰到一个关于表单问题,在用户提交表单时候浏览器会提示是否保存帐号 如果点击保存,在退出帐号切换其他帐号时,浏览器会自动为表单填充数据,为了解决这个自动填充问题时, 主要分2个思路来解决,一个 ...

  8. Yii 1开发日记 -- 后台搜索功能下拉及关联表搜索

    Yii 1 实现后台搜索,效果如下: 一. 下拉搜索: 1.模型中和常规的一样 if (isset($_GET['agency']['status']) && $_GET['agenc ...

  9. window.print() 打印页面部分内容的方法

    用 JavaScript 实现页面数据的打印 : 主要是用到了一个 print() 函数 , 该函数将会打印整个 web 页面 body 内的所有 html 数据 ! 使用方法为 window.pri ...

  10. iOS之开发小技巧

    1.xcode如何添加快捷代码 xcode添加快捷代码 属性 2.cocoapods安装 cocoapods安装 3.iOS真机调试 真机调试 4.命令行自动打包 xcrun -sdk iphoneo ...