web项目中各种路径的获取
2013-11-02 14:49
197 查看
以工程名为/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的安装路径
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
访问的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的安装路径
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
相关文章推荐
- 杭电水题之4143
- 跨平台中对于 条件量(condition) 的包装
- ubuntu,debian root密码忘记破解
- 简洁极致的Vim配置
- 解决SQLServer 2008 日志无法收缩,收缩后大小不改变
- MODBUS异常码
- 素数
- JDBC的Statement和PreparedStatement区别
- C++: Socket工作流程图(WinSock)
- JAVA学习日记
- MyEclipse中使用SVN插件方法
- pureftp服务器搭建
- 忐忑
- Google地图 经度 纬度 到 像素索引值 的转换公式
- String三个空的区别
- CentOS 6.4安装bind-9.8.2最新版(DNS服务器)
- Release Notes for XE5
- CSS 关于IE6 margin 为负数 负值的时候 正常显示的方法
- 跨平台中对于 信号量(semaphore) 的包装
- ios中@class和 #import,两种方式的讨论