转自: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. Tomcat 内存优化设置

    vi /tomcat7.0/bin/catalina.sh 开发环境 #!/bin/sh JAVA_OPTS='-Xms128m -Xmx512m -XX:PermSize=128m' 服务器: #! ...

  2. gulp工具rename

    gulp 对文件批量重命名 gulp-rename重命名 var gulp = require('gulp'); var rename = require("gulp-rename" ...

  3. c#在字符串中计算加减乘除...

                DataTable dt = new DataTable();             Response.Write(dt.Compute("1+1*5", ...

  4. css3 perspective perspective-origin属性的理解

    perspective字面意思是:透视. 在w3school中它的解释为:设置元素被查看位置的视图:通俗讲,就是我们看看一个物体的所处的视角,近大远小.就比如我们正对着电脑:当我无限贴近电脑屏幕的时候 ...

  5. [SHOI2008]堵塞的交通traffic

    我是萌萌的传送门 这题说白了就是一个支持加边和删边的图连通性维护,不过鉴于图的特殊性,可以直接线段树(听说标算就是这个--). 然而我人比较懒,不想思考怎么线段树,于是乎写了一发分治并查集,1A我真是 ...

  6. Python读写csv

    读取csv: http://blog.csdn.net/lixiang0522/article/details/7755059 读取Excel: http://www.cnblogs.com/lhj5 ...

  7. 使用KRPano资源分析工具强力加密KRPano项目(XML防破解,切片图保护,JS反调试)

    软件交流群:571171251(软件免费版本在群内提供) krpano技术交流群:551278936(软件免费版本在群内提供) 最新博客地址:blog.turenlong.com 限时下载地址:htt ...

  8. ".NET Compact Framework v2.0 could not be found."

    参考: http://blog.csdn.net/godcyx/article/details/7348431 问题原因: That's a known issue where VS can't di ...

  9. C 语言Struct 实现运行类型识别 RTTI

    通过RTTI,能够通过基类的指针或引用来检索其所指对象的实际类型.c++通过下面两个操作符提供RTTI. (1)typeid:返回指针或引用所指对象的实际类型.    (2)dynamic_cast: ...

  10. 在cygwin部署hadoop出现的问题:$ ./bin/hadoop version 显示错误: 找不到或无法加载主类 org.apache.hadoop.util.VersionInfo

    解决方案 找到hadoop主目录的bin文件夹下的hadoop文件,将倒数第二行 exec "$JAVA" $JAVA_HEAP_MAX $HADOOP_OPTS $CLASS & ...