转:getContextPath、getServletPath、getRequestURI的区别
2016-02-18 11:17
656 查看
假定你的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、在程序中的应用如下:
Java代码
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()可以返回当前页面所在的应用的名字;
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、在程序中的应用如下:
Java代码
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()可以返回当前页面所在的应用的名字;
相关文章推荐
- 你也来学设计模式 之 Builder模式
- UIViewAnimationOptions
- Usage of forceLayout(), requestLayout() and invalidate()
- android 传统蓝牙Bluetooth联通性
- iOS环信3.0集成 (二)UI文件集成
- UITextField
- 使用UITextField 键盘遮盖输入框
- Java调用XML的方法:DocumentBuilderFactory
- 四、UiDevice API 详细介绍
- 为什么Java 两个Integer 中1000==1000为false而100==100为true?
- UI操作 解决方案
- MySQL中出现 Field 'tid' doesn't have a default value错误
- UIUC大学之Coursera课程Text Retrieval and Search Engines:Week 1 Quiz
- UIUC大学之Coursera课程Text Retrieval and Search Engines:Week 1 Practice Quiz
- UIPickerViewController 打开相机 图库 相册
- 哥伦比亚大学Coursera课程Natural Language Processing:Quiz 1: covers material from weeks 1 and 2
- 哥伦比亚大学Coursera课程Natural Language Processing:Quiz 2: covers material from weeks 3 and 4
- iOS CUICatalog: Invalid asset name supplied 错误
- android之bluetooth详解(2基础)
- android之Bluetooth详解