以工程名为/DemoWeb为例: 
访问的jsp为:http://localhost:8080/DemoWeb/test/index.jsp

1 JSP中获得当前应用的相对路径和绝对路径 
(1)得到工程名:request.getContextPath() 
结果:/DemoWeb

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

(3)得到IE地址栏地址:request.getRequestURL() 
结果:http://localhost:8080/DemoWeb/test/testpath.jsp

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

(5)得到页面所在服务器的全路径(实际的路径):application.getRealPath("testpath.jsp") 
结果:D:\Develop Files\apache-tomcat-5.5.15\apache-tomcat-5.5.15\webapps\DemoWeb\testpath.jsp 
D:\Develop Files\apache-tomcat-5.5.15\apache-tomcat-5.5.15为tomcat的安装路径

(6) Web应用中有各种获取path或URI,URL的方法,假设网页访问地址:

http://localhost:8080/tradeload/TestServlet

Web应用context: /tradeload

  1. request.getContextPath()= /tradeload
  2. request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()= http://localhost:8080
  3. request.getRequestURL() = http://localhost:8080/tradeload/TestServlet
  4. request.getRequestURI() = /tradeload/TestServlet
  5. request.getPathInfo() = null
  6. request.getServletPath() = /TestServlet
  7. getServletContext().getRealPath('/') = C:\server\glassfish\domains\domain1\applications\j2ee-modules\tradeload\

2.java 的Class中获得相对路径,绝对路径的方法

(1)类的绝对路径: 
System.out.println(TestPath.class.getResource("/").getPath()); 
结果:/E:/workspace/workspace_tcc/DemoWeb/WebRoot/WEB-INF/classes/

System.out.println(TestPath.class.getResource("")); 
结果:file:/E:/workspace/workspace_tcc/DemoWeb/WebRoot/WEB-INF/classes/demo1/

(2)得到工程的路径:System.getProperty("user.dir") 

结果:E:\workspace\workspace_tcc\DemoWeb

(3)得到项目部署的绝对路径:

//获取运行项目名称
WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
ServletContext sc =wac.getServletContext();
String projectName = sc.getContextPath();

//获取运行项目路径 即tomcat下的项目路径(默认情况下)
//tomcat conf server.xml配置文件若有配置项目路径<Context path="" docBase="D:\seed" reloadable="false" />,则是该docBase的路径
WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
ServletContext servletContext = webApplicationContext.getServletContext();
String projectPath=servletContext.getRealPath("/").replace("\\", "/");

最新文章

  1. Chrome 开发工具之Sources
  2. Ubuntu16.04安装有道词典
  3. python 安装nltk,使用(英文分词处理,词干化等)(Green VPN)
  4. 在本地创建angular-ui/bootstrap项目
  5. 黑客语(Leet)
  6. Progress Control with Text
  7. miniUI子窗口调父窗口方法
  8. FlexSlider插件的详细设置参数 http://www.woothemes.com/flexslider/ -----幻灯片插件
  9. 关于String s = new String(&quot;xyz&quot;); 创建几个对象的问题
  10. Kendo UI开发教程(14): Kendo MVVM 数据绑定(三) Click
  11. Webots入门(二)-build up a controller
  12. Python3 面向对象程序设计(类)
  13. HTML5之Canvas画圆形
  14. git忽视修改的文件
  15. 机器学习数据集,主数据集不能通过,人脸数据集介绍,从r包中获取数据集,中国河流数据集
  16. jQuery事件--keypress([[data],fn])和trigger(type,[data])
  17. Lua和C++交互 学习记录之九:在Lua中以面向对象的方式使用C++注册的类
  18. artDialog 弹窗提示
  19. .NET4.0中使用4.5中的 async/await 功能实现异步
  20. 【MFC】CHtmlView或WebBrowser禁止脚本错误提示

热门文章

  1. NBUT 1457 莫队算法 离散化
  2. css 使图片水平垂直居中
  3. CodeForces - 148D Bag of mice
  4. Linux哲学思想--基本法则
  5. Ubuntu下deb文件及tgz文件安装
  6. wireshark lua脚本
  7. 了解一下C++输入和输出的概念
  8. 中国计算机学会CCF推荐国际学术会议
  9. Tomcat8安装, 安全配置与性能优化(转)
  10. 【linux使用】bash shell命令行常用快捷键 (转载)