拼裝網頁路徑

2021-07-12 03:30:05 字數 804 閱讀 8104

<%

string path = request.getcontextpath();

string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/";

%>

這個語句是用來拼裝當前網頁的相對路徑的。是用來表明當前頁面的相對路徑所使用的根路徑的。

比如,頁面內部有乙個連線,完整的路徑應該是 http://localhost:80/myblog/authen/login.do。其中http://server/是伺服器的基本路徑,myblog是當前應用程式的名字,那麼,我的根路徑應該是那麼http://localhost:80/myblog/。

request.getschema()可以返回當前頁面使用的協議,就是上面例子中的「http」

request.getservername()可以返回當前頁面所在的伺服器的名字,就是上面例子中的「localhost」

request.getserverport()可以返回當前頁面所在的伺服器使用的埠,就是80,

request.getcontextpath()可以返回當前頁面所在的應用的名字,就是上面例子中的myblog

這四個拼裝起來,就是當前應用的根路徑了。

有了這個以後,我的頁面內容的連線,我不想寫全路徑,我只要寫 authen/login.do就可以了。伺服器會自動把指定的路徑和頁面內的相對路徑拼裝起來,組成完整路徑。如果沒有這個,那麼我頁面的連鏈結就必須寫全路徑,否則伺服器會找不到。

拼裝當前網頁的相對路徑

先放 string path request.getcontextpath string basepath request.getscheme request.getservername request.getserverport path 下面解釋一下 的作用 request.getcontext...

關於路徑拼裝的解釋

string path request.getcontextpath string basepath request.getscheme request.getservername request.getserverport path 這個語句是用來拼裝當前網頁的相對路徑的。是用來表明當前頁面的相對...

網頁物件路徑

路徑是指檔案在伺服器中的儲存位置。我們在使用計算機時要找到所需的檔案時就必須知道檔案的絕對路徑,例如,只要看到這個路徑d web logo.gif,我們就知道 logo.gif 檔案是在d盤的 web目錄下。類似於這樣完整的描述檔案位置的路徑就是絕對路徑。絕對路徑的概念 整個檔案的完整路徑,比如在當...