相對路徑 絕對路徑 詳細分析

這裏寫圖片描述

以Qqq.html 裏的代碼爲例 對於他來說

1.絕對路徑:D:/UP/123.text

2.相對路徑:123.text

2.1當前目錄

 ./ 當前目錄  也可以直接忽略不寫

例如 :./123.text   就等於 D:/UP/123.text
例如 :123.text     就等於 D:/UP/123.text

2.2向上返回一級

../   向上返回一級 

例如 :../123.text   就等於 D:/123.text

2.2向上返回兩級 (三級,四級都一個道理)

../../ 類推就是向上返回兩級   例如   D:/Q/W/Qqq.html
例如 :../../ 123.text   就等於 D:/123.text

2.3項目的根目錄

/  就是項目的根目錄

你的項目路徑是D:/app    你的html的路徑是D:/app/UP/Qqq.html

例如 :/123.text   就等於 D:/app/123.text

j2ee

假定你的web application 名稱爲news,你在瀏覽器中輸入請求路徑:

http://localhost:8080/news/main/list.jsp

1.1 request.getContextPath()

http://localhost:8080/news/main/list.jsp

打印結果:/news
1.2 request.getServletPath()

http://localhost:8080/news/main/list.jsp

打印結果:/main/list.jsp
1.3 request.getRequestURI()

http://localhost:8080/news/main/list.jsp

打印結果:/news/main/list.jsp
1.4 request.getRealPath("/")

http://localhost:8080/news/main/list.jsp

打印結果:F:\Tomcat 6.0\webapps\news\test
1.5 request.getScheme() 

http://localhost:8080/news/main/list.jsp

等到的是協議名稱,默認是http
1.6 request.getServerName() 
得到的是在服務器的配置文件中配置的服務器名稱

http://localhost:8080/news/main/list.jsp

 比如:localhost 或者.baidu.com 等等
1.7 request.getServerPort() 
得到的是服務器的配置文件中配置的端口號 

http://localhost:8080/news/main/list.jsp

比如 8080等等
http://localhost:8080/news/main/list.jsp

   request.getScheme() + ":////" +

     http                  ://     

+ request.getServerName()+ ":" 

     localhost               :

+ request.getServerPort() 

     8080

+ request.getContextPath()+ "/";

     /news                       /

(附錄增加更新)從request獲取各種路徑總結

equest.getRealPath() 這個方法已經不推薦使用了,代替方法是:
request.getSession().getServletContext().getRealPath()

在servlet裏用this.getServletContect().getRealPath()
在struts裏用this.getServlet().getServletContext().getRealPath()
在Action裏用ServletActionContext.getRequest().getRealPath();
以上三個獲得都是當前運行文件在服務器上的絕對路徑

從request獲取各種路徑總結
request.getRealPath(“url”); // 虛擬目錄映射爲實際目錄

request.getRealPath(“./”); // 網頁所在的目錄

request.getRealPath(“../”); // 網頁所在目錄的上一層目錄

request.getContextPath(); // 應用的web目錄的名稱

獲取Web項目的全路徑
String strDirPath = request.getSession().getServletContext().getRealPath(“/”);

以工程名爲TEST爲例:

(1)得到包含工程名的當前頁面全路徑:request.getRequestURI()
結果:/TEST/test.jsp

(2)得到工程名:request.getContextPath()
結果:/TEST

(3)得到當前頁面所在目錄下全名稱:request.getServletPath()
結果:如果頁面在jsp目錄下 /TEST/jsp/test.jsp

(4)得到頁面所在服務器的全路徑:application.getRealPath(“頁面.jsp”)
結果:D:\resin\webapps\TEST\test.jsp

(5)得到頁面所在服務器的絕對路徑:absPath=new java.io.File(application.getRealPath(request.getRequestURI())).getParent();
結果:D:\resin\webapps\TEST

2.在類中取得路徑:

(1)類的絕對路徑:Class.class.getClass().getResource(“/”).getPath()
結果:/D:/TEST/WebRoot/WEB-INF/classes/pack/

(2)得到工程的路徑:System.getProperty(“user.dir”)
結果:D:\TEST

3.在Servlet中取得路徑:

(1)得到工程目錄:request.getSession().getServletContext().getRealPath(“”) 參數可具體到包名。
結果:E:\Tomcat\webapps\TEST

(2)得到IE地址欄地址:request.getRequestURL()
結果:http://localhost:8080/TEST/test

(3)得到相對地址:request.getRequestURI()
結果:/TEST/test

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章