jsp Request獲取url信息的各種方法對比
從Request對象中可以獲取各種路徑信息,以下例子:
假設(shè)請求的頁面是index.jsp,項目是WebDemo,則在index.jsp中獲取有關(guān)request對象的各種路徑信息如下
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String remoteAddress=request.getRemoteAddr();
String servletPath=request.getServletPath();
String realPath=request.getRealPath("/");
String remoteUser=request.getRemoteUser();
String requestURI=request.getRequestURI();
out.println("path:"+path+"<br>");
out.println("basePath:"+basePath+"<br>");
out.println("remoteAddr:"+remoteAddress+"<br>");
out.println("servletPath:"+servletPath+"<br>");
out.println("realPath:"+realPath+"<br>");
out.println("remoteUser:"+remoteUser+"<br>");
out.println("requestURI:"+requestURI+"<br>");
結(jié)果:
path:/WebDemo basePath:http://localhost:8683/WebDemo/ remoteAddr:127.0.0.1 servletPath:/index.jsp realPath:D:\apache-tomcat-6.0.13\webapps\WebDemo\ remoteUser:null requestURI:/WebDemo/index.jsp
從上不難看出request各個對應(yīng)方法所代表的含義
從request獲取各種路徑總結(jié):
request.getRealPath("url");//虛擬目錄映射為實際目錄
request.getRealPath("./");//網(wǎng)頁所在的目錄
request.getRealPath("../");//網(wǎng)頁所在目錄的上一層目錄
假定你的web application(web應(yīng)用)名稱為news,你的瀏覽器中輸入請求路徑:http://localhost:8080/uploading/load.jsp
request.getContextPath() => /uploading
request.getServletPath() => /load.jsp
request.getRequestURL() => http://localhost:8080/uploading/load.jsp
request.getRealPath("/") => F:\learn\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\uploading\
現(xiàn)在request.getRealPath("/") 這個方法已經(jīng)不推薦使用了
可以使用
ServletContext.getRealPath(java.lang.String) instead. request.getSession().getServletContext().getRealPath() 得到工程文件的實際物理路徑,也就是絕對地址
//Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request // eg./manage/editExam.domethod=goExamSet&type=U String url = request.getRequestURI(); //The returned URL contains a protocol, server name, port number, and server path, but it does not include query string parameters //eg. http://127.0.0.1:8080/manage/editExam.domethod=goExamSet&type=U StringBuffer url_buffer = request.getRequestURL();
HttpServletRequest 的這兩種方法都只能得到不包含參數(shù)的請求url,區(qū)別如下:
1 前者返回相對路徑,后者返回完整路徑
2 前者返回string ,后者返回stringbuffer
得到完整請求url可以通過如下方法,getQueryString()得到的是url后面的參數(shù)串,和前者相加就是帶參數(shù)的請求路徑了
String queryString = request.getQueryString(); ring fullPath = url + queryString; // 或者是url_buffer.toString()+queryString;
以上就是小編為大家?guī)淼膉sp Request獲取url信息的各種方法對比的全部內(nèi)容了,希望對大家有所幫助,多多支持腳本之家~
相關(guān)文章
通過JDBC連接oracle數(shù)據(jù)庫的十大技巧
通過JDBC連接oracle數(shù)據(jù)庫的十大技巧...2006-10-10
jsp實現(xiàn)將動態(tài)網(wǎng)頁轉(zhuǎn)換成靜態(tài)頁面的方法
這篇文章主要介紹了jsp實現(xiàn)將動態(tài)網(wǎng)頁轉(zhuǎn)換成靜態(tài)頁面的方法,較為詳細的分析了jsp將動態(tài)網(wǎng)頁轉(zhuǎn)換成靜態(tài)網(wǎng)頁的原理與相關(guān)實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10
JSP中用回車監(jiān)聽按鈕事件兼容火狐 IE等主流瀏覽器
這篇文章主要介紹了用回車監(jiān)聽按鈕事件并且兼容火狐、IE等主流瀏覽器 ,需要的朋友可以參考下2014-04-04
Hibernate識別數(shù)據(jù)庫特有字段實例詳解
這篇文章主要介紹了Hibernate識別數(shù)據(jù)庫特有字段實例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06
JSP中實現(xiàn)系統(tǒng)登錄后的退出原理及代碼
首先要在你登錄的頁面的某個地方,寫上一個超級鏈接,讓它鏈接到exit.jsp頁面去,然后新建一個exit.jsp頁面在其body中寫上如下代碼,即可以實現(xiàn)登錄后的退出,感興趣的朋友可以了解下啊,或許對你有所幫助2013-01-01
struts2 action跳轉(zhuǎn)調(diào)用另一個程序
主要為了在一個Action成功后跳轉(zhuǎn)調(diào)用另一個程序,需要的朋友可以參考下2012-11-11

