最近对于request中的几种“路径”有点混淆,查找网上资源都没有很好的总结,希望此文章能够帮助我理解一下这几种“路径”。
+++++++++++++++++++++++++++++++++++++++++++++++++

本文章主要讨论以下几种request获取路径的方法:

request.getServletPath()
request.getPathInfo()
request.getContextPath()
request.getRequestURI()
request.getRequestURL()
request.getServletContext().getRealPath()

以一个简单的例子说明:
web.xml配置(注意此处的url-pattern项)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"><display-name>aab</display-name><welcome-file-list><welcome-file>a.jsp</welcome-file></welcome-file-list><servlet><servlet-name>test</servlet-name><servlet-class>com.java.test.TestServlet</servlet-class></servlet><servlet-mapping><servlet-name>test</servlet-name><url-pattern>/*</url-pattern><!-- 注意此处 --></servlet-mapping></web-app>

TestServlet.java文件:

package com.java.test;import java.io.IOException;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class TestServlet extends HttpServlet{@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {this.doPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("servletPath:"+req.getServletPath());System.out.println("contextPath:"+req.getContextPath());System.out.println("contextPath2:"+req.getServletContext().getContextPath());System.out.println("pageInfo:"+req.getPathInfo());System.out.println("uri:"+req.getRequestURI());System.out.println("url:"+req.getRequestURL());System.out.println("realPath:"+req.getServletContext().getRealPath("/"));}
}

此时请求http://localhost:8080/testweb (url-pattern=/*)
打印出来的值为:

servletPath:
contextPath:/testweb
contextPath2:/testweb
pageInfo:null
uri:/testweb
url:http://localhost:8080/testweb
realPath:G:\java\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\testweb\

请求http://localhost:8080/testweb/abc 打印的值为:

servletPath:
contextPath:/testweb
contextPath2:/testweb
pageInfo:/abc
uri:/testweb/abc
url:http://localhost:8080/testweb/abc
realPath:G:\java\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\testweb\

当我们修改web.xml为如下时(注意url-pattern的改变):

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"><display-name>aab</display-name><welcome-file-list><welcome-file>a.jsp</welcome-file></welcome-file-list><servlet><servlet-name>test</servlet-name><servlet-class>com.java.test.TestServlet</servlet-class></servlet><servlet-mapping><servlet-name>test</servlet-name><url-pattern>/abc/def/*</url-pattern><!-- 注意此处 --></servlet-mapping></web-app>

请求http://localhost:8080/testweb/abc/def/ghi/test.html (url-pattern=/abc/def/*)
打印的值为:

servletPath:/abc/def
contextPath:/testweb
contextPath2:/testweb
pageInfo:/ghi/test.html
uri:/testweb/abc/def/ghi/test.html
url:http://localhost:8080/testweb/abc/def/ghi/test.html
realPath:G:\java\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\testweb\

通过观察打印结果,我们可以总结:

  1. getServletPath():获取能够与“url-pattern”中匹配的路径,注意是完全匹配的部分,*的部分不包括。
  2. getPageInfo():与getServletPath()获取的路径互补,能够得到的是“url-pattern”中*d的路径部分
  3. getContextPath():获取项目的根路径
  4. getRequestURI:获取根路径到地址结尾
  5. getRequestURL:获取请求的地址链接(浏览器中输入的地址)
  6. getServletContext().getRealPath(“/”):获取“/”在机器中的实际地址
  7. getScheme():获取的是使用的协议(http 或https)
  8. getProtocol():获取的是协议的名称(HTTP/1.11)
  9. getServerName():获取的是域名(xxx.com)
  10. getLocalName:获取到的是IP

关于request.getServletPath(),request.getContextPath()的总结相关推荐

  1. request.getServletPath()和request.getPathInfo()用法

    为什么80%的码农都做不了架构师?>>>    在 Web 中,我们通常需要获取 URL 相对于 Webapp 的路径,主要是下面的几个方法: request.getServletP ...

  2. basePath = request.getScheme()+://+request.getServerName()+:+r

    basePath = request.getScheme()+"://"+request.getServerName()+":"+r (2014-06-30 1 ...

  3. JSP中的:request.getScheme()+://+request.getServerName()+:+request.getServer

    String path = request.getContextPath();  String basePath = request.getScheme()+"://"+reque ...

  4. Request、Request.Form和Request.QueryString的区别

    Request.Request.Form和Request.QueryString的区别 Request.Form:获取以POST方式提交的数据(接收Form提交来的数据): Request.Query ...

  5. request.POST / request.body区别

    django request.POST / request.body 当request.POST没有值 需要考虑下面两个要求 1.如果请求头中的: Content-Type: application/ ...

  6. Unable to execute HTTP request:The request signature we calculated does not match the signature you

    使用"OSSClient"的上传文件方法"putObject()"遇到报错 报错信息: Unable to execute HTTP request:The r ...

  7. request.form()和request()和request.data的区别

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

  8. Request.From,Request.QueryString转对象

    From表单转化为对象 public static T RequestFormEntities<T>(HttpRequestBase request) where T : new(){T ...

  9. request Form request QueryString

    request Form  取控件的name request QueryString取地址栏上的参数

最新文章

  1. zabbix4.0添加mysql报警_部署监控三剑客 Zabbix4.0 监控以及告警机制
  2. C# 正则表达式过滤危险HTML
  3. 驰骋工作流引擎设置消息收听
  4. 安装mysql 环境变量_win10系统安装mysql数据库后配置环境变量的图文教程
  5. 关于 SAP Spartacus feature library 里的 _index.scss 文件,和神奇的下划线省略行为
  6. (转)B2B2C,从营销的角度,来理解SaaS
  7. Spring Boot学习总结(16)——为什么说Java程序员到了必须掌握Spring boot的时候了?
  8. 贺利坚老师汇编课程37笔记:运用栈加两层循环之把六个字符串里的字母都改写成大写字母
  9. Android 蓝牙开发(五)OPP接收文件
  10. Git 输入命令git branch -a 后怎么返回
  11. modbus rtu功能码理解
  12. 如何调整eclipse字体大小
  13. 基于stm32单片机外文文献_基于STM32的智能家居系统设计毕业论文+任务书+开题报告+文献综述+外文翻译及原文+程序+原理图+参考资料+答辩PPT+仿真设计...
  14. 加壳、脱壳以及如何病毒免杀技术与原理
  15. CRM系统的营销工具亮点
  16. 六爻金钱卦 手工摇卦方法
  17. 合租服务器禁用 fsockopen 函数,部分程序不正常的解决办法。
  18. WIN10 DOS命令
  19. php 判断来源 微信客户端_使用PHP判断是否为微信、支付宝等移动设备访问代码...
  20. Deep Learning for Generic Object Detection: A Survey -- 目标检测综述总结

热门文章

  1. mybatis配置文件属性的详解和db.propertis文件的使用
  2. ES6-Set集合的创建
  3. Spring数据分析思维课
  4. Windows8Windows Phone 做一个图片效果
  5. shell_之_find(查找)
  6. flutter 输入框限制输入 数字、小数
  7. java并发编程之缓存一致性问题
  8. webpack是什么?为什么要用webpack(一个小白的感想)
  9. 成员/方法/属性/私有
  10. bootstrap-table前端修改后台传来的数据重新进行渲染