转载: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. 删除 mac 垃圾桶内清除不掉的文件

    命令行 内 $ sudo rm -rf ~/.Trash/

  2. 【sparkStreaming】SparkStream的创建

    DStream编程数据模型 DStream(Discretized Stream)作为Spark Streaming的基础抽象,它代表持续性的数据流. 这些数据流既可以通过外部输入源赖获取,也可以通过 ...

  3. 【Wannafly挑战赛9-B】数一数

    链接:https://www.nowcoder.net/acm/contest/71/B 题目就不贴了.. 设res[i]为第i行的最终结果,可以想到,res[i]为0或不为0.长度不是最短的字符串r ...

  4. 在一个Activity中启动另一个Activity

    一.新建一个空的工程 二.添加一个Activity并命名为BAty 三.在activity_main.xml中添加一个按钮,设置id号为btnStartB <Button android:lay ...

  5. elasticsearch聚合案例--分组、求最大值再求最大值的均值

    一.需求 A.B.C代表3个用户,第二列代表各自的得分,求A.B.C的最好成绩以及A.B.C最好成绩的均值 A 10 A 11 A 13 B 11 B 11 B 12 C 10 C 10 C 11 C ...

  6. Hadoop集群中节点角色定义

    Hadoop分别从两个角度将主机划分为两种角色. 最基本的划分原则为Master和Slave,即主人和奴隶: 第一,从HDFS的角度,将主机划分为NameNode和DataNode(在分布式文件系统中 ...

  7. 剑指offer-第六章面试中的各项能力(二叉树的深度)

    题目:1:输入一个二叉树,求二叉树的深度.从根节点开始最长的路径. 思路:我们可以考虑用递归,求最长的路径实际上就是求根节点的左右子树中较长的一个然后再加上1. 题目2:输入一颗二叉树的根节点,判断该 ...

  8. 一次SQL Server 10054 Troubleshooting

    问题 对某个库新增了一个订阅节点,然后需要把一些应用切到新订阅库,以分散负载.当应用切换后,有一个应用每次启动不到30秒,总是报超时的错误,而error log中又没有任何记录: Timeout ex ...

  9. 阿里云接口异常-Can not find endpoint to access

    最近在做公司的资产盘点,需要请求阿里云的接口获取公司的云服务器信息.在获取实例列表的过程中,通过异常机制捕获了 Can not find endpoint to access 这个错误.经过多次排查, ...

  10. 一分钟理解js闭包

    什么是闭包?先看一段代码: ? 1 2 3 4 5 6 7 8 9 10 function a(){   var n = 0;   function inc() {     n++;     cons ...