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("/"));
request.getRealPath("/")已经不建议使用。
getRealPath();
返回一个字符串,包含一个给定虚拟路径的真实路径。
struts2中:
ServletContext ctx=ServletActionContext.getServletContext();
String path=ctx.getRealPath("/");
String path1=ctx.getRealPath("/files/view.jsp");
输出:path为D:\Javasoftware\apache-tomcat-7.0.70\apache-tomcat-7.0.70\webapps\struts2-1\
path1为D:\Javasoftware\apache-tomcat-7.0.70\apache-tomcat-7.0.70\webapps\struts2-1\files\view.jsp
\files\view.jsp这部分就是虚拟路径
D:\Javasoftware\apache-tomcat-7.0.70\apache-tomcat-7.0.70\webapps\struts2-1\ 为项目的绝对路径
servlet中:
private ServletConfig config;
public void init(ServletConfig config) throws ServletException {
this.config=config;}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext ctx=config.getServletContext();
String temp=ctx.getRealPath("/");
}
打印结果:F:\Tomcat 6.0\webapps\news\test
注:
URI=contextPath+servletPath
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 ...
- request的getServletPath(),getContextPath(),getRequestURI(),getRealPath("/")区别
假定你的web application 名称为news,你在浏览器中输入请求路径: http://localhost:8080/news/main/list.jsp 则执行下面向行代码后打印出如下结果 ...
- 转:getContextPath、getServletPath、getRequestURI的区别
假定你的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 ...
随机推荐
- VS2013的一些常用快捷键
1.回到上一个光标位置/前进到下一个光标位置 1)回到上一个光标位置:使用组合键“Ctrl + -”: 2)前进到下一个光标位置:“Ctrl + Shift + - ”. 2.复制/剪切/删除整行代码 ...
- Java中Enum类型的序列化(转)
在Java中,对Enum类型的序列化与其他对象类型的序列化有所不同,今天就来看看到底有什么不同.下面先来看下在Java中,我们定义的Enum在被编译之后是长成什么样子的. Java代码: Java代码 ...
- Handler基本概念
Handler基本概念: Handler主要用于异步消息的处理:当发出一个消息之后,首先进入一个消息队列,发送消息的函数即刻返回,而另外一个部分逐个的在消息队列中将消息取出,然后对消息进行出来,就是发 ...
- petapoco IsNew
// Check if a poco represents a new record public bool IsNew(string primaryKeyName, object poco) { v ...
- 【VB.NET】类绑定控件,实现文本框快捷键全选
Public Class KeyBinder Public Sub BindControl(ByRef CControl As TextBox) AddHandler CControl.KeyDown ...
- myEclipse
破解myEclipse 建立一个java项目,将reg.java放入,并且运行在控制台 输入账户 回车就会出现 序列号 菜单栏--->myeclipse-->substription in ...
- iOS开发~CocoaPods使用详细说明【转】
一.概要 iOS开发时,项目中会引用许多第三方库,CocoaPods(https://github.com/CocoaPods/CocoaPods)可以用来方便的统一管理这些第三方库. 二.安装 由于 ...
- tinyxml学习2
在TinyXML中,根据XML的各种元素来定义了一些类: TiXmlBase:整个TinyXML模型的基类. TiXmlAttribute:对应于XML中的元素的属性. TiXmlNode:对应于DO ...
- oracle中的timestamp字段的值乱码问题修改
我的解决方案: 直接新增一个系统变量: key值为:NLS_TIMESTAMP_FORMATvalue的值为:YYYY-MM-DD HH24:MI:SSFF6 其它解决方案: 在登录PLSQL之后,查 ...
- String 及其数组的相关问题
由其他类型转String一般用三种方法 方法1:采用 Object.toString()方法 请看下面的例子: Object object = getObject(); System.out.prin ...