假定你的web application 名称为news,你在浏览器中输入请求路径: 
http://localhost:8080/news/main/list.jsp 
则执行下面向行代码后打印出如下结果: 
1、 System.out.println(request.getContextPath()); //可返回站点的根路径。也就是项目的名字 
打印结果:/news 
   2、System.out.println(request.getServletPath()); 
打印结果:/main/list.jsp 
3、 System.out.println(request.getRequestURI()); 
打印结果:/news/main/list.jsp 
4、 System.out.println(request.getRealPath("/")); 
打印结果:F:\Tomcat 6.0\webapps\news\test

-------------------------------------------------------------------

1、request.getRequestURL()

返回的是完整的url,包括Http协议,端口号,servlet名字和映射路径,但它不包含请求参数。
2、request.getRequestURI()

得到的是request URL的部分值,并且web容器没有decode过的

3、request.getContextPath() 
返回 the context of the request.

4、request.getServletPath() 
返回调用servlet部分的url.

5、request.getQueryString() 
返回url路径?后面的查询字符串

示例:

当前url:http://localhost:8080/CarsiLogCenter_new/idpstat.jsp?action=idp.sptopn

request.getRequestURL() http://localhost:8080/CarsiLogCenter_new/idpstat.jsp
request.getRequestURI()  /CarsiLogCenter_new/idpstat.jsp
request.getContextPath() /CarsiLogCenter_new
request.getServletPath() /idpstat.jsp

request.getQueryString() action=idp.sptopn

1、request.getScheme() 返回当前链接使用的协议;比如,一般应用返回http;SSL返回https;

2、在程序中的应用如下:

  1. String path = request.getContextPath();
  2. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  3. <base href="<%=basePath%>">

3、复习一下request 的常用方法:

request.getSchema()可以返回当前页面使用的协议,http 或是 https;

request.getServerName()可以返回当前页面所在的服务器的名字;

request.getServerPort()可以返回当前页面所在的服务器使用的端口,就是80;

request.getContextPath()可以返回当前页面所在的应用的名字;

转:getContextPath、getServletPath、getRequestURI的区别的更多相关文章

  1. getServletPath getRequestURI getRequestURL区别

    getContextPath:/test               //上下文,类似工程名 getServletPath:/test.jsp getRequestURI:/test/test.jsp ...

  2. Servlet的getContextPath(), getServletPath(), getRequestURI(), getRealPath("/")

    假定web application 名称为news,你在浏览器中输入请求路径: http://localhost:8080/news/main/list.jsp 则执行下面向行代码后打印出如下结果: ...

  3. getContextPath和getRealPath的区别-----其实主要区别就是相对路径和绝对路径

    getContextPath和getRealPath的区别 其实主要区别就是相对路径和绝对路径 https://blog.csdn.net/zsmj_2011/article/details/4121 ...

  4. getContextPath、getServletPath、getRequestURI的区别

    假定你的web application 项目名称为news,你在浏览器中输入请求路径: http://localhost:8080/news/main/list.jsp 则执行下面向行代码后打印出如下 ...

  5. request的getServletPath(),getContextPath(),getRequestURI(),getRealPath("/")区别

    假定你的web application 名称为news,你在浏览器中输入请求路径: http://localhost:8080/news/main/list.jsp 则执行下面向行代码后打印出如下结果 ...

  6. HttpServletRequest 中 getRequestURL和getRequestURI的区别

    比如说有这样的一个页面 test1.jsp======================= <a href ="test.jsp?name=wf">跳转到test2.js ...

  7. getRequestURI getRequestURL 区别

    参考 https://blog.csdn.net/gavid0124/article/details/45390999/ request.getRequestURL() 返回全路径 request.g ...

  8. request.getRequestURL()和request.getRequestURI()的区别

    request.getRequestURL() 返回全路径 request.getRequestURI() 返回除去host(域名或者ip)部分的路径 request.getContextPath() ...

  9. 关于J2EE里面getContextPath()和getRealPath()的区别

    一直老搞不清楚这两个方法的区别,只知道他们都是拿来获取地址的.今天特意写了个小demo试了一下,代码如下: @Override protected void service(HttpServletRe ...

随机推荐

  1. c#数和二叉树

    树(Tree)是 n(n≥0)个相同类型的数据元素的有限集合.树中的数据元素叫结点(Node).n=0 的树称为空树(Empty Tree):对于 n>0 的任意非空树 T 有: (1)有且仅有 ...

  2. fiddler 抓取 逍遥安卓模拟器 https包

    1.打开fiddler,进行相关设置 Tools--Fiddler Options 接下来进行客户端网络配置 1 查看电脑ip地址,ipconfig 逍遥游模拟器中使用自带的浏览器,访问192.168 ...

  3. Maven命令行使用 mvn clean package

    先把命令行切换到Maven项目的根目录,比如:/d/xxxwork/java/maven-test,然后执行命令:  mvn clean package 执行结果如下: [INFO] Scanning ...

  4. webservice jaxws header验证

    @WebService @HandlerChain public class UserService { ... } package com.xx.ws.header; import org.w3c. ...

  5. HashCode的理解

    一.hashcode是什么 要理解hashcode首先要理解hash表这个概念 1. 哈希表 hash表也称散列表(Hash table),是根据关键码值(Key value)而直接进行访问的数据结构 ...

  6. Usage of “symmetrical” and “symmetric”

    What is the appropriate usage of "symmetrical" and "symmetric" (using the geomet ...

  7. swift OC混编工程,xcode断点调试,控制台左侧只有变量名称不显示值,右侧输入po命令,打印除一堆提示

    断点调试 (lldb) po 变量名warning: Swift error in module 项目名.Debug info from this module will be unavailable ...

  8. PostgreSQL+pgpool-II复制方案

    目录 PostgreSQL+pgpool-II复制方案 1. Pgpool-II介绍 2. pgpool-II安装 2.1 安装pgpool-II yum源,并安装pgpool-II 2.2 添加Pg ...

  9. mysql中GROUP_CONCAT的使用

    现在有三个表,结构如下: cate表 CREATE TABLE `cate` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'id', ...

  10. c#/vb调用c编写的标准dll

    准备: 首先打开vc++ 6.0新建工程,选择Win32 Dynamic Link-Library,命名为stdLibrary 新建library.cpp文件,内容如下 #include <st ...