这篇教程不错:http://zjutsoft.iteye.com/blog/1084260

自己试验如下:

System.out.println("-----------------servlet-----------------");
System.out.println("realpath null:"+request.getRealPath(""));
System.out.println("realpath index.jsp:"+request.getRealPath("index.jsp"));
System.out.println("realpath ./:"+request.getRealPath("./"));
System.out.println("realpath ../:"+request.getRealPath("../"));
System.out.println("contextpath:"+request.getContextPath());
System.out.println("web项目的全路径:"+request.getSession().getServletContext().getRealPath("/"));
System.out.println("URI:"+request.getRequestURI());
System.out.println("URL:"+request.getRequestURL());
System.out.println("servletpath:"+request.getServletPath());
//System.out.println("页面在服务器的绝对路径:"+new java.io.File(application.getRealPath(request.getRequestURI())).getParent()); System.out.println("~~~~~~~~~~~~~~~页面~~~~~~~~~~~~~~~~~");
System.out.println("realpath null:"+request.getRealPath(""));
System.out.println("realpath index.jsp:"+request.getRealPath("index.jsp"));
System.out.println("realpath ./:"+request.getRealPath("./"));
System.out.println("realpath ../:"+request.getRealPath("../"));
System.out.println("contextpath:"+request.getContextPath());
System.out.println("web项目的全路径:"+request.getSession().getServletContext().getRealPath("/"));
System.out.println("URI:"+request.getRequestURI());
System.out.println("URL:"+request.getRequestURL());
System.out.println("servletpath:"+request.getServletPath());
System.out.println("页面在服务器的绝对路径:"+new java.io.File(application.getRealPath(request.getRequestURI())).getParent()); //结果
-----------------servlet-----------------
realpath null:D:\eclipse-jee-luna-SR1-win32\eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\jyxt
realpath index.jsp:D:\eclipse-jee-luna-SR1-win32\eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\jyxt\index.jsp
realpath ./:D:\eclipse-jee-luna-SR1-win32\eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\jyxt\.
realpath ../:D:\eclipse-jee-luna-SR1-win32\eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\jyxt\..
contextpath:/jyxt
web项目的全路径:D:\eclipse-jee-luna-SR1-win32\eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\jyxt\
URI:/jyxt/hello
URL:http://localhost:8080/jyxt/hello
servletpath:/hello
~~~~~~~~~~~~~~~页面~~~~~~~~~~~~~~~~~
realpath null:D:\eclipse-jee-luna-SR1-win32\eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\jyxt
realpath index.jsp:D:\eclipse-jee-luna-SR1-win32\eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\jyxt\index.jsp
realpath ./:D:\eclipse-jee-luna-SR1-win32\eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\jyxt\.
realpath ../:D:\eclipse-jee-luna-SR1-win32\eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\jyxt\..
contextpath:/jyxt
web项目的全路径:D:\eclipse-jee-luna-SR1-win32\eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\jyxt\
URI:/jyxt/WEB-INF/view/index.jsp
URL:http://localhost:8080/jyxt/WEB-INF/view/index.jsp
servletpath:/WEB-INF/view/index.jsp
页面在服务器的绝对路径:D:\eclipse-jee-luna-SR1-win32\eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\jyxt\jyxt\WEB-INF\view

在类中获取路径先不看,其他的只有10种方法。

两种应用场景:.jsp和servlet中。jsp中realPath被抛弃,不过返回值跟servlet中的一样,getRealPath("./   ../")返回值都不太对;页面在服务器的绝对路径 就不能用了。

getContextPath() 和 .getSession().getServletContext().getRealPath("/") 在.jsp和servlet中返回值都一样;URI,URL,servletpath都不一样。

//http
System.out.println("scheme:"+request.getScheme());
//localhost
System.out.println("name:"+request.getServerName());
//8080
System.out.println("port:"+request.getServerPort());

常用方法

在servlet中:getContextPath(), URI, URL, servletPath(),
在jsp中:getContextPath()

jsp request 获取路径的更多相关文章

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

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

  2. request获取路径方式

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

  3. request获取路径

    1.request.getRequestURL() 返回的是完整的url,包括Http协议,端口号,servlet名字和映射路径,但它不包含请求参数. 2.request.getRequestURI( ...

  4. [转]java中通过request获取路径中的不同信息

    原文地址:http://blog.csdn.net/lv_shijun/article/details/40819859 aa为工程中的项目名 bb为webRoot下的文件夹 1.request.ge ...

  5. jsp中获取路径信息的方法

    今天在看代码时,发现程序使用了 request.getScheme() .不明白是什么意思,查了一下.结果整理如下: 1.request.getScheme() 返回当前链接使用的协议:一般应用返回h ...

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

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

  7. Java获取路径的方法分析详解(Application/Web)

    1.利用System.getProperty()函数获取当前路径: System.getProperty("user.dir");//user.dir用户当前的工作目录,输出:D: ...

  8. request 获取各种路径

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

  9. request获取各种路径

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

随机推荐

  1. Linux SSH远程文件/目录 传输

    Linux SSH远程文件/目录传输命令scp 2010年08月6日 上午 | 作者:VPS侦探 相信各位VPSer在使用VPS时会经常在不同VPS间互相备份数据或者转移数据,大部分情况下VPS上都已 ...

  2. 《Linux内核精髓:精通Linux内核必会的75个绝技》一HACK #2 如何编译Linux内核

    HACK #2 如何编译Linux内核 本节介绍编译Linux内核的方法.当发现bug而修改源代码或者添加新功能时,就需要对内核进行重新编译,生成二进制映像文件.另外,如果想要使用发布版内核中无效的功 ...

  3. yum安装nagois

    多少年前就装过了,今天再来用yum装一次,都忘干净了~~ 主监控机:CentOS 6.5 192.168.0.105被监控机:CentOS 6.5 192.168.0.107 主监控机设置:1.安装e ...

  4. 转载spring restemplate

    什么是RestTemplate? RestTemplate是Spring提供的用于访问Rest服务的客户端,RestTemplate提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效 ...

  5. InputStream转化为String

    参考:https://blog.csdn.net/lmy86263/article/details/60479350 eg:  InputStream in = PropertiesUtils.cla ...

  6. 「小程序JAVA实战」小程序的页面重定向(60)

    转自:https://idig8.com/2018/09/24/xiaochengxujavashizhanxiaochengxudeyemianzhongdingxiang59/ 在我们正常的浏览网 ...

  7. sql中合并列方法

    方法一:创建合并列函数 -------创建一个方法---------- CREATE FUNCTION dbo.Role_Name(@AdminID int) ) AS BEGIN ) SET @r ...

  8. fekit 搭建

    sudo apt-get install npmsudo apt-get install nodejs 保证npm配置正确 sudo  npm config set registry http://r ...

  9. go_组合接口

    main函数入口 package main import ( "fmt" "learngo/retriever/mock" "learngo/retr ...

  10. 用 AutoHotKey 随时记录所想

    别被标题咋呼了,其实很简单,按下快捷键自动打开指定文本文档,自动加上当前时间日期,适合像我这种无聊的人记录生活. ;Alt+X 调出 !X:: ;获取当前日期时间并保存到剪贴板 d = @rhinoc ...