转载:http://blog.csdn.net/piaoxuan1987/article/details/8541839

equest.getRealPath() 这个方法已经不推荐使用了,代替方法是:

request.getSession().getServletContext().getRealPath()

从Request对象中可以获取各种路径信息,以下例子:
假设请求的页面是index.jsp,项目是WebDemo,则在index.jsp中获取有关request对象的各种路径信息如下
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String remoteAddress=request.getRemoteAddr();
String servletPath=request.getServletPath();
String realPath=request.getRealPath("/");
String remoteUser=request.getRemoteUser();
String requestURI=request.getRequestURI();
out.println("path:"+path+"<br>");
out.println("basePath:"+basePath+"<br>");
out.println("remoteAddr:"+remoteAddress+"<br>");
out.println("servletPath:"+servletPath+"<br>");
out.println("realPath:"+realPath+"<br>");
out.println("remoteUser:"+remoteUser+"<br>");
out.println("requestURI:"+requestURI+"<br>");
结果:
path:/WebDemo
basePath:http://localhost:8683/WebDemo/
remoteAddr:127.0.0.1
servletPath:/index.jsp
realPath:D:\apache-tomcat-6.0.13\webapps\WebDemo\
remoteUser:null
requestURI:/WebDemo/index.jsp
从上不难看出request各个对应方法所代表的含义

参考servlet中的接口:

request.getScheme();
返回的协议名称,默认是http

request.getServerName()
返回的是你浏览器中显示的主机名,你自己试一下就知道了

getServerPort()
获取服务器端口号

例如:

<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/servlet/test/*</url-pattern>
</servlet-mapping>

dwr为工程名
http://localhost:8080/dwr/servlet/test/joejoe1991/a.html?name=test
getPathInfo() 返回的仍然是:
"/joejoe1991/a.html" ,而并不包括后面的"?name=test";

在servlet里用this.getServletContect().getRealPath()

在struts里用this.getServlet().getServletContext().getRealPath()

在Action里用ServletActionContext.getRequest().getRealPath();

以上三个获得都是当前运行文件在服务器上的绝对路径

从request获取各种路径总结 
request.getRealPath("url"); // 虚拟目录映射为实际目录

request.getRealPath("./");    // 网页所在的目录

request.getRealPath("../"); // 网页所在目录的上一层目录

request.getContextPath();    // 应用的web目录的名称

如http://localhost:7001/bookStore/ 
/bookStore/ => [contextPath] (request.getContextPath())

获取Web项目的全路径 
String strDirPath = request.getSession().getServletContext().getRealPath("/");

以工程名为TEST为例:

(1)得到包含工程名的当前页面全路径:request.getRequestURI() 
结果:/TEST/test.jsp

(2)得到工程名:request.getContextPath() 
结果:/TEST

(3)得到当前页面所在目录下全名称:request.getServletPath() 
结果:如果页面在jsp目录下 /TEST/jsp/test.jsp

(4)得到页面所在服务器的全路径:application.getRealPath("页面.jsp") 
结果:D:\resin\webapps\TEST\test.jsp

(5)得到页面所在服务器的绝对路径:absPath=new java.io.File(application.getRealPath(request.getRequestURI())).getParent(); 
结果:D:\resin\webapps\TEST

2.在类中取得路径:

(1)类的绝对路径:Class.class.getClass().getResource("/").getPath() 
结果:/D:/TEST/WebRoot/WEB-INF/classes/pack/

(2)得到工程的路径:System.getProperty("user.dir") 
结果:D:\TEST

3.在Servlet中取得路径:

(1)得到工程目录:request.getSession().getServletContext().getRealPath("") 参数可具体到包名。 
结果:E:\Tomcat\webapps\TEST

(2)得到IE地址栏地址:request.getRequestURL() 
结果:http://localhost:8080/TEST/test

(3)得到相对地址:request.getRequestURI() 
结果:/TEST/test

从request获取各种路径总结 request.getRealPath("url")的更多相关文章

  1. request 获取各种路径

    从request获取各种路径总结 request.getRealPath("url"); // 虚拟目录映射为实际目录 request.getRealPath("./&q ...

  2. request获取各种路径

    equest.getRealPath() 这个方法已经不推荐使用了,代替方法是: request.getSession().getServletContext().getRealPath() 在ser ...

  3. 从request获取各种路径总结

    一.获得都是当前运行文件在服务器上的绝对路径 在servlet里用: this.getServletContext().getRealPath() 在struts用: this.getServlet( ...

  4. request获取各种路径总结、页面跳转总结。

    页面跳转总结 JSP中response.sendRedirect()与request.getRequestDispatcher().forward(request,response)这两个对象都可以使 ...

  5. jsp获取绝对路径----${pageContext.request.contextPath}

    JSP取得绝对路径 在JavaWeb开发中,常使用绝对路径的方式来引入JavaScript和CSS文件,这样可以避免因为目录变动导致引入文件找不到的情况,常用的做法如下: 一.使用${pageCont ...

  6. jsp页面中引入文件路径问题的解决方案(使用request获取项目路径)【原创】

    在项目页面中,总会引入一些js和css,相对路径or绝对路径的选择就显得至关重要了!下面是项目中遇到的问题和解决方案,做一下记录! 环境: myEclipse创建工程,使用jsp+css+js,项目目 ...

  7. 直接用request.setAttribute()会报错,在这之前应该先让request获取ServletActionContext.getRequest();方法 // request.getAttribute同理

    正确流程应该是 import javax.servlet.http.HttpServletRequest; HttpServletRequst request = ServletActionConte ...

  8. request获取路径方式

    从request获取各种路径总结 request.getRealPath("url"); // 虚拟目录映射为实际目录 request.getRealPath("./&q ...

  9. jsp Request获取url信息的各种方法比较

    从Request对象中可以获取各种路径信息,以下例子: 假设请求的页面是index.jsp,项目是WebDemo,则在index.jsp中获取有关request对象的各种路径信息如下 String p ...

随机推荐

  1. activity启动模式之singleTask

    activity启动模式之singleTask 一.简介 如果另外一个应用调用了C2,C2在栈底,如果这个程序里面再嗲用C1,C3,C2,那么这个C2就是调用onNewIntant的,C1和C3都被销 ...

  2. Django进阶Model篇008 - 使用原生sql

    注意:使用原生sql的方式主要目的是解决一些很复杂的sql不能用ORM的方式写出的问题. 一.extra:结果集修改器-一种提供额外查询参数的机制 二.执行原始sql并返回模型实例 三.直接执行自定义 ...

  3. vue-cli 脚手架项目简介(一) - package.json

    vue-cli是用来生成 vue项目的命令行工具,它的使用方法是这样的: vue init <template-name> <project-name>第二个参数 templa ...

  4. Ceph配置项动态变更机制浅析

    转自:https://www.ustack.com/blog/ceph%e9%85%8d%e7%bd%ae%e9%a1%b9%e5%8a%a8%e6%80%81%e5%8f%98%e6%9b%b4%e ...

  5. angularJS中directive父子组件的数据交互

    angularJS中directive父子组件的数据交互 1. 使用共享 scope 的时候,可以直接从父 scope 中共享属性.使用隔离 scope 的时候,无法从父 scope 中共享属性.在 ...

  6. EL表达式 分割字符串 ,forEach定次循环

    后台取出来的是字符串  以 a,b,c,   的形式  前台要将字符串中的“,”去掉 ,并forEach重新拼接 list.labelsName不用加${} <c:set value=" ...

  7. ionic2——开发利器之Visual Studio Code 常用快捷键

    主命令框 F1 或 Ctrl+Shift+P: 打开命令面板.在打开的输入框内,可以输入任何命令,例如: 按一下 Backspace 会进入到 Ctrl+P 模式 在 Ctrl+P 下输入 >  ...

  8. #define GPFCON (* (volatile unsigned long * )0x56000050 )

    int a; int *p; p = &a; *p = 0x100; //a=0x100 p = (int *)0x56000050; *p =0x100; *( ( int * ) 0x56 ...

  9. Prism5.0新内容(纯汉语版)

    Prism 5.0 包含很多新东西,新的快速入门示例,新的范例,更新的文档,Prism类库代码的改变.用来解决已有问题,回应社区所提的需求.(这句话跟 What's New in Prism Libr ...

  10. OC-SEL 和 Class

    [认识选择器]============================================ SEL 1.选择器(SEL)是一个变量类型. 2.选择器用来装成员消息(成员方法) people ...