1.基本概念的理解
絕對(duì)路徑:絕對(duì)路徑就是你的主頁上的文件或目錄在硬盤上真正的路徑,(URL和物理路徑)例如: C:\xyz\test.txt 代表了test.txt文件的絕對(duì)路徑。http://www.sun.com/index.htm也代表了一個(gè) URL絕對(duì)路徑。
相對(duì)路徑:相對(duì)與某個(gè)基準(zhǔn)目錄的路徑。包含Web的相對(duì)路徑(HTML中的相對(duì)目錄),例如:在 Servlet中,“/”代表Web應(yīng)用的跟目錄。和物理路徑的相對(duì)表示。例如:“。/” 代表當(dāng)前目錄, “。./”代表上級(jí)目錄。這種類似的表示,也是屬于相對(duì)路徑。 另外關(guān)于URI,URL,URN等內(nèi)容,請(qǐng)參考RFC相關(guān)文檔標(biāo)準(zhǔn)。 RFC 2396: Uniform Resource Identifiers (URI): Generic Syntax, (http://www.ietf.org/rfc/rfc2396.txt)
2.關(guān)于JSP/Servlet中的相對(duì)路徑和絕對(duì)路徑。
2.1服務(wù)器端的地址
服務(wù)器端的相對(duì)地址指的是相對(duì)于你的web應(yīng)用的地址,這個(gè)地址是在服務(wù)器端解析的 (不同于html和javascript中的相對(duì)地址,他們是由客戶端瀏覽器解析的)也就是說這時(shí)候 在jsp和servlet中的相對(duì)地址應(yīng)該是相對(duì)于你的web應(yīng)用,即相對(duì)于http://192.168.0.1/webapp/的。
其用到的地方有: forward:servlet中的request.getRequestDispatcher(address);這個(gè)address是 在服務(wù)器端解析的,所以,你要forward到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中
2.2、客戶端的地址 所有的html頁面中的相對(duì)地址都是相對(duì)于服務(wù)器根目錄(http://192.168.0.1/)的, 而不是(跟目錄下的該Web應(yīng)用的目錄)http://192.168.0.1/webapp/的。 Html中的form表單的action屬性的地址應(yīng)該是相對(duì)于服務(wù)器根目錄(http://192.168.0.1/)的, 所以,如果提交到a.jsp為:action=“/webapp/user/a.jsp”或action=“”/user/a.jsp;
提交到servlet為actiom=“/webapp/handleservlet”
Javascript也是在客戶端解析的,所以其相對(duì)路徑和form表單一樣。
因此,一般情況下,在JSP/HTML頁面等引用的CSS,Javascript.Action等屬性前面最好都加上 ,以確保所引用的文件都屬于Web應(yīng)用中的目錄。 另外,應(yīng)該盡量避免使用類似“。”,“。/”,“。./。./”等類似的相對(duì)該文件位置的相對(duì)路徑,這樣 當(dāng)文件移動(dòng)時(shí),很容易出問題。
3. JSP/Servlet中獲得當(dāng)前應(yīng)用的相對(duì)路徑和絕對(duì)路徑
3.1 JSP中獲得當(dāng)前應(yīng)用的相對(duì)路徑和絕對(duì)路徑 根目錄所對(duì)應(yīng)的絕對(duì)路徑:request.getRequestURI() 文件的絕對(duì)路徑 :application.getRealPath(request.getRequestURI());
當(dāng)前web應(yīng)用的絕對(duì)路徑 :application.getRealPath(“/”);
取得請(qǐng)求文件的上層目錄:new File(application.getRealPath(request.getRequestURI())).getParent()
3.2 Servlet中獲得當(dāng)前應(yīng)用的相對(duì)路徑和絕對(duì)路徑 根目錄所對(duì)應(yīng)的絕對(duì)路徑:request.getServletPath();
文件的絕對(duì)路徑 :request.getSession().getServletContext().getRealPath (request.getRequestURI())
當(dāng)前web應(yīng)用的絕對(duì)路徑 :servletConfig.getServletContext().getRealPath(“/”);
(ServletContext對(duì)象獲得幾種方式: javax.servlet.http.HttpSession.getServletContext() javax.servlet.jsp.PageContext.getServletContext() javax.servlet.ServletConfig.getServletContext() )
4.java 的Class中獲得相對(duì)路徑,絕對(duì)路徑的方法
4.1單獨(dú)的Java類中獲得絕對(duì)路徑
根據(jù)java.io.File的Doc文擋,可知: 默認(rèn)情況下new File(“/”)代表的目錄為:
System.getProperty(“user.dir”)。 一下程序獲得執(zhí)行類的當(dāng)前路徑
package org.cheng.file;
import java.io.File;
public class FileTest {
public static void main(String[] args) throws Exception { System.out.println(Thread.currentThread().getContextClassLoader().getResource(“”)); System.out.println(FileTest.class.getClassLoader().getResource(“”)); System.out.println(ClassLoader.getSystemResource(“”)); System.out.println(FileTest.class.getResource(“”));
System.out.println(FileTest.class.getResource(“/”));
//Class文件所在路徑
System.out.println(new File(“/”).getAbsolutePath());
System.out.println(System.getProperty(“user.dir”));
}
}
4.2服務(wù)器中的Java類獲得當(dāng)前路徑(來自網(wǎng)絡(luò))
(1).Weblogic WebApplication的系統(tǒng)文件根目錄是你的weblogic安裝所在根目錄。 例如:如果你的weblogic安裝在c:\bea\weblogic700.。..。 那么,你的文件根路徑就是c:\。 所以,有兩種方式能夠讓你訪問你的服務(wù)器端的文件:
a.使用絕對(duì)路徑: 比如將你的參數(shù)文件放在c:\yourconfig\yourconf.properties, 直接使用 new FileInputStream(“yourconfig/yourconf.properties”);
b.使用相對(duì)路徑: 相對(duì)路徑的根目錄就是你的webapplication的根路徑,即WEB-INF的上一級(jí)目錄,將你的參數(shù)文件放 在yourwebapp\yourconfig\yourconf.properties, 這樣使用: new FileInputStream(“。/yourconfig/yourconf.properties”); 這兩種方式均可,自己選擇。
(2).Tomcat 在類中輸出System.getProperty(“user.dir”);顯示的是%Tomcat_Home%/bin
(3).Resin 不是你的JSP放的相對(duì)路徑,是JSP引擎執(zhí)行這個(gè)JSP編譯成SERVLET 的路徑為根。比如用新建文件法測(cè)試File f = new File(“a.htm”); 這個(gè)a.htm在resin的安裝目錄下 (4)。如何讀相對(duì)路徑哪? 在Java文件中g(shù)etResource或getResourceAsStream均可 例:getClass().getResourceAsStream(filePath);//filePath可以是“/filename”,這里的/代表web 發(fā)布根路徑下WEB-INF/classes 默認(rèn)使用該方法的路徑是:WEB-INF/classes。已經(jīng)在Tomcat中測(cè)試。
5.讀取文件時(shí)的相對(duì)路徑,避免硬編碼和絕對(duì)路徑的使用。