一、普通類
①
▲
String path1 = this.getClass().getClassLoader().getResource(".").getPath(); //取得根目錄(classes)路徑/F:/zzd/java/test/p_test/build/classes/
▲
getResource("XXX") //取得根目錄下的目錄、文件的路徑(XX為目錄、文件名/F:/zzd/java/test/p_test/build/classes/Notepad.class),更深的目錄、文件不行
▲
getResource("/") //不行
String path = path1.substring(path1.indexOf('/') + 1, path1.indexOf("/WEB-INF")) + "/tp/" + tpb.getWjm(); //必須path1.indexOf('/') + 1而不能1,因?yàn)閜ath1可能以file:/開頭
▲
getClass().getName(); //包括包的類名
▲
getClass().getPackage().getName(); //包名
二、Servlet
①
▲
String filePath = this.getServletContext().getRealPath("/"); //只能得到web目錄F:\zzd\java\softdown\build\web\,其他無效
▲
getRealPath(".") //無效
▲
getRealPath("XXX") //無效
②
▲
String path = this.getClass().getClassLoader().getResource("/").getPath(); //得到類目錄/F:/zzd/java/softdown/build/web/WEB-INF/classes/
▲
getResource("XXX") //(同“一”)
▲
getResource(".") //取得/D:/Tomcat/common/classes/
String s = request.getServletPath(); //得到本Servlet的web相對(duì)路徑:/pathTest
String s = request.getRequestURI(); //得到本Servlet的web絕對(duì)路徑:/strutsTest/pathTest
三、Action
①
String filePath = this.getServlet().getServletContext().getRealPath("/"); //得到web目錄F:\zzd\java\softdown\build\web\
②
▲
String path = this.getClass().getClassLoader().getResource("/").getPath(); //取得根目錄(classes)路徑/F:/zzd/java/test/strutsTest2/build/web/WEB- INF/classes/
▲
getResource("XXX") //同“一”,XXX可為classes目錄下的文件,更深目錄下的文件須寫出完整的相對(duì)根目錄(classes)的路徑,如:com.myapp.struts
String path = p1.substring(p1.indexOf('/') + 1, p1.lastIndexOf('/')) + "相對(duì)路徑"; //得到的路徑以'/'開頭,所以需p1.indexOf('/') + 1處理;用p1.lastIndexOf('/')去掉最后的"/XXX"
三、JSP
只能得到web目錄F:\zzd\java\test\strutsTest\build\web\,其他無效
①
▲
String path = pageContext.getServletContext().getRealPath(""); //得到web目錄F:\zzd\java\test\strutsTest\build\web(沒有最后的“\”)
▲
String path = pageContext.getServletContext().getRealPath("/"); //得到web目錄F:\zzd\java\test\strutsTest\build\web
②
▲
String strPathFile2 = application.getRealPath(""); //得到web目錄F:\zzd\java\test\strutsTest\build\web(沒有最后的“\”)
▲
String strPathFile2 = application.getRealPath("/"); //得到web目錄F:\zzd\java\test\strutsTest\build\web\
=======================
1、服務(wù)器端的地址
服務(wù)器端的相對(duì)地址指的是相對(duì)于你的web應(yīng)用的地址,這個(gè)地址是在服務(wù)器端解析的,也就是說這時(shí)候在jsp和servlet中的相對(duì)地址應(yīng)該是相對(duì)于你的web應(yīng)用,即相對(duì)于http: //192.168.0.1/webapp/的。
其用到的地方有:
forwarder:
ervlet中的request.getRequestDispatcher(address);
這個(gè)address是在服務(wù)器端解析的,所以,你要 forwarder到a.jsp應(yīng)該這么寫:request.getRequestDispatcher(“/user/a.jsp”)這個(gè)/相對(duì)于當(dāng)前的web應(yīng)用webapp,其絕對(duì)地址就是:http://192.168.0.1/webapp/user/a.jsp。
sendRedirect:
在jsp中<%response.sendRedirect("/rtccp/user/a.jsp");%>
struts的配置文件中,forward路徑應(yīng)當(dāng)以"/"開頭
=====
對(duì)class程序文件來說,classes目錄是根目錄
posted on 2007-12-26 21:09
cccp21 閱讀(1144)
評(píng)論(0) 編輯 收藏