首页 技术 正文
技术 2022年11月6日
0 收藏 670 点赞 832 浏览 2033 个字

以工程名为/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(“\\”, “/”);

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,023
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,360
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,143
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,774
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,852