转:getContextPath、getServletPath、getRequestURI的区别
假定你的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、在程序中的应用如下:
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- <base href="<%=basePath%>">
3、复习一下request 的常用方法:
request.getSchema()可以返回当前页面使用的协议,http 或是 https;
request.getServerName()可以返回当前页面所在的服务器的名字;
request.getServerPort()可以返回当前页面所在的服务器使用的端口,就是80;
request.getContextPath()可以返回当前页面所在的应用的名字;
转:getContextPath、getServletPath、getRequestURI的区别的更多相关文章
- getServletPath getRequestURI getRequestURL区别
getContextPath:/test //上下文,类似工程名 getServletPath:/test.jsp getRequestURI:/test/test.jsp ...
- Servlet的getContextPath(), getServletPath(), getRequestURI(), getRealPath("/")
假定web application 名称为news,你在浏览器中输入请求路径: http://localhost:8080/news/main/list.jsp 则执行下面向行代码后打印出如下结果: ...
- getContextPath和getRealPath的区别-----其实主要区别就是相对路径和绝对路径
getContextPath和getRealPath的区别 其实主要区别就是相对路径和绝对路径 https://blog.csdn.net/zsmj_2011/article/details/4121 ...
- getContextPath、getServletPath、getRequestURI的区别
假定你的web application 项目名称为news,你在浏览器中输入请求路径: http://localhost:8080/news/main/list.jsp 则执行下面向行代码后打印出如下 ...
- request的getServletPath(),getContextPath(),getRequestURI(),getRealPath("/")区别
假定你的web application 名称为news,你在浏览器中输入请求路径: http://localhost:8080/news/main/list.jsp 则执行下面向行代码后打印出如下结果 ...
- HttpServletRequest 中 getRequestURL和getRequestURI的区别
比如说有这样的一个页面 test1.jsp======================= <a href ="test.jsp?name=wf">跳转到test2.js ...
- getRequestURI getRequestURL 区别
参考 https://blog.csdn.net/gavid0124/article/details/45390999/ request.getRequestURL() 返回全路径 request.g ...
- request.getRequestURL()和request.getRequestURI()的区别
request.getRequestURL() 返回全路径 request.getRequestURI() 返回除去host(域名或者ip)部分的路径 request.getContextPath() ...
- 关于J2EE里面getContextPath()和getRealPath()的区别
一直老搞不清楚这两个方法的区别,只知道他们都是拿来获取地址的.今天特意写了个小demo试了一下,代码如下: @Override protected void service(HttpServletRe ...
随机推荐
- linux下的计算器
(1)bc bc在默认的情况下是个交互式的指令.在bc工作环境下,可以使用以下计算符号:+ 加法 - 减法 * 乘法 / 除法 ^ 指数 % 余数如: 3+4;5*2;5^2;18/4 &l ...
- spring学习笔记(二)
Spring的Bean管理:(注解方式) Spring的AOP:XML方式 Spring的AOP:注解方式 1.Spring的Bean管理的中常用的注解: * @Controller :WEB层 ...
- ubuntu下搭建testlink
环境配置: 1. 安装mysql 教程网上找 2. 安装apache sudo apt-get install apache2 重启apache服务 sudo /etc/init.d/apache2 ...
- 配置Tomcat 7 Gzip
<Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" ...
- android笔记:Service
服务:在后台运行,没有界面的组件. 服务生命周期如下: 两种启动方式: 1.startService(): onCreate()-->onStartCommand()-->onDestro ...
- 56. Merge Intervals (Array; Sort)
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- Food(最大流)
Food http://acm.hdu.edu.cn/showproblem.php?pid=4292 Time Limit: 2000/1000 MS (Java/Others) Memory ...
- Ecshop模板目录介绍[xyytit]
一. 模板系统介绍 Ecshop模板系统使用我们自行研发的模板控制系统和著名的PHP开源模板程序Smarty组合而成.为了方便用户开发模板,还使用了Dreamweaver的模板和库的功能Smarty中 ...
- 8-导弹拦截一(n^2 and nlogn)
/*某国为了防御敌国的导弹袭击,研发出一套导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发拦截炮弹能够到达任意的高度,但是以后每一发拦截炮弹都不能高于前一发的高度.某天,雷达捕捉到敌国的多 ...
- 记录java版本不兼容的坑,(kafka运行报错)
启动kafka报错 错误原因是: 由较高版本的jdk编译的java class文件 试图在较低版本的jvm上运行的报错 解决办法是: 查看java版本 C:\Users\Administrator&g ...