關於
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 和
String basePath=http://localhost:8080/ 等同
開始學java的時候看不懂為什么這么寫,今天復習的時候才搞懂:這個語句是用來拼接當前網頁的相對路徑的。
<base herf="...">從來表明當前頁面的相對路徑所使用的根路徑,也就是項目名稱
比如,頁面內部有一個連接,完整的路徑應該是 http://localhost:80/Info_System/user/user_info.jsp
其中http://server/是服務器的基本路徑,Info_System是當前應用程序的名字,那么,我的根路徑應該是那么http://localhost:8080/Info_System/。
用這個base,服務器就可以動態的將指定路徑和頁面的相對路徑拼裝起來,形成完整的路徑。
1.request.getSchema();可以返回當前頁面所使用的協議,就是"http"
2.request.getServerName();返回當前頁面所在服務器的名字,就是上面例子中的"localhost"
3.request.getServerPort();返回當前頁面所在服務器的端口號,就是上面例子中的"8085"
4.request.getContextPath();返回當前頁面所在的應用的名字,就是上面例子中的"Info_System"
當前頁面路徑是:http://localhost:80/Info_System/uploadAttachs/attachs_list.jsp
根據上面的介紹,那么我在當前頁面中有跳轉為: <li><a href="<%=basePath%>user/user_info.jsp" class="information_off"></a></li>就應該這樣寫。