【转】request.getServletPath()和request.getPathInfo()用法
转自: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()用法的更多相关文章
- (转)关于request.getServletPath(),request.getContextPath()的总结
文章完全转载自 : https://blog.csdn.net/qq_27770257/article/details/79438987 最近对于request中的几种“路径”有点混淆,查找网上资源都 ...
- request.getServletPath(),request.getContextPath()
2018-11-24 16:34:33 1. getServletPath():获取能够与“url-pattern”中匹配的路径,注意是完全匹配的部分,*的部分不包括. 2. getPageInfo ...
- 关于request.getServletPath(),request.getContextPath()的总结
1. getServletPath():获取能够与“url-pattern”中匹配的路径,注意是完全匹配的部分,*的部分不包括. 2.getContextPath():获取项目的根路径
- request.getRequestURI() 、request.getRequestURL() 、request.getContextPath()、request.getServletPath()区别
request.getRequestURI() /jqueryWeb/resources/request.jsprequest.getRequestURL() http://localhost:808 ...
- request.getRequestURL()和request.getRequestURI()的区别
request.getRequestURL() 返回全路径 request.getRequestURI() 返回除去host(域名或者ip)部分的路径 request.getContextPath() ...
- C#中 Request, Request.params , Request.querystring , Request.Form 区别 与联系用法
C#中 Request, Request.params , Request.querystring , Request.Form 区别 与联系用法? Request.params , Request ...
- [转载]request.getServletPath()方法
假定你的web application 名称为news,你在浏览器中输入请求路径: http://localhost:8080/news/main/list.jsp 则执行下面向行代码后打印出如下结果 ...
- Request、Request.Form、Request.QueryString 用法的区别
Request.Form:获取以POST方式提交的数据. Request.QueryString:获取地址栏参数(以GET方式提交的数据). Request:包含以上两种方式(优先获取GET方式提交的 ...
- ASP.NET中Request.ApplicationPath、Request.FilePath、Request.Path、.Request.MapPath
1.Request.ApplicationPath->当前应用的目录 2.Request.FilePath->对应于iis的虚拟目录 如 URL http://mockte.com/1 ...
随机推荐
- PYTHON 写函数,检查获取传入列表或元组对象的所有奇数位索引对应的元素,并将其作为新列表返回给调用者
def a3(arg): ret = [ ] for i in range(len(arg)): if i % 2 == 1: ret.append(arg[i]) else: pass return ...
- Android中锁定文件的方法
androidSDK中并没有锁定文件相关的api. 但是android是基于linux操作系统的,linux比较底层,灵活性也更大,为了实现锁定文件的效果,大概有以下几种办法: 用chmod命令修改文 ...
- Application对象、ViewState对象、分页展示--2017年1月4日
Application对象 存储 Application 变量 Application["application名称"] = "application的值"; ...
- 移动端H5页面高清多屏适配方案
背景 开发移动端H5页面 面对不同分辨率的手机 面对不同屏幕尺寸的手机 视觉稿 在前端开发之前,视觉MM会给我们一个psd文件,称之为视觉稿. 对于移动端开发而言,为了做到页面高清的效果,视觉稿的规范 ...
- Mosquitto pub/sub服务实现代码浅析-主体框架
Mosquitto 是一个IBM 开源pub/sub订阅发布协议 MQTT 的一个单机版实现(目前也只有单机版),MQTT主打轻便,比较适用于移动设备等上面,花费流量少,解析代价低.相对于XMPP等来 ...
- 第20讲 HOOK和数据库编程
1,安装钩子过程可以通过SetWindowsHookEx函数来完成 2,得到当前线程ID,可以用GetCurrentThreadId 3,移除钩子可以用UnhookWindowsHookEx函数 4, ...
- sh6.脚本磁盘分区格式化
练习1. 写一个脚本,通过ping 命令测试192.168.0.100到192.168.0.254之间的所有主机是否在线, 如果在线,就显示"ip is up."IP为真实IP地址 ...
- swift错误 Expressions are not allowed at the top level
``` ... earlier we said top-level code isn't allowed in most of your app's source files. The excepti ...
- Centos7下安装mysql5.7.16
mysql的安装(root用户下) 从官网下载软件 linux下必须安装系统对应的版本,多少位 必须安装的是:server,client 但是我可不管要安装那个插件,我们直接使用bundle版本(就是 ...
- Hamilton四元数群的表示
Hamilton四元数群$Q_8=\mathbb H=\{\pm e,\pm i,\pm j,\pm k\}$满足如下运算法则: $e$为单位元且同号得正.异号得负,此外$e=i^2=j^2=k^2, ...