a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > JSP > 正文

jsp Request獲取url信息的各種方法對比

2024-09-05 00:22:40
字體:
來源:轉載
供稿:網友

從Request對象中可以獲取各種路徑信息,以下例子: 

假設請求的頁面是index.jsp,項目是WebDemo,則在index.jsp中獲取有關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>"); 

結果: 

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各個對應方法所代表的含義 

從request獲取各種路徑總結: 

request.getRealPath("url");//虛擬目錄映射為實際目錄 request.getRealPath("./");//網頁所在的目錄 request.getRealPath("../");//網頁所在目錄的上一層目錄 

假定你的web application(web應用)名稱為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/ 

現在request.getRealPath("/") 這個方法已經不推薦使用了 

可以使用 

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 的這兩種方法都只能得到不包含參數的請求url,區別如下: 

1 前者返回相對路徑,后者返回完整路徑 

2 前者返回string ,后者返回stringbuffer 

得到完整請求url可以通過如下方法,getQueryString()得到的是url后面的參數串,和前者相加就是帶參數的請求路徑了 

 String queryString = request.getQueryString(); ring fullPath = url + queryString;  // 或者是url_buffer.toString()+queryString; 

以上就是小編為大家帶來的jsp Request獲取url信息的各種方法對比的全部內容了,希望對大家有所幫助,多多支持VeVb武林網~


注:相關教程知識閱讀請移步到JSP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 色网在线视频 | 精品国产一区二区三区av小说 | 欧美一极视频 | 国产成人精品免费视频大全最热 | 高清一区二区 | 高清国产一区二区三区四区五区 | 久久人人爽人人爽 | 一区 | 日韩成人精品 | 一级视频在线观看 | 国产精品一区二区久久久久 | 欧美日韩高清一区 | 6080yy精品一区二区三区 | 欧美色呦呦 | 播放毛片 | 九九九视频精品 | 国产精品久久久久久亚洲毛片 | 久久国产视频网站 | 一区二区三区高清 | 国产视频一区二区 | 欧美日韩一区二区三区在线观看 | 99re免费视频精品全部 | 精品成人在线 | 亚洲九九精品 | 亚洲精品www久久久久久广东 | 91精品国产欧美一区二区成人 | 国产一区二区三区四区三区 | 国产三区四区 | 精品在线不卡 | 国产精品美女在线观看直播 | 久久这里只有精品首页 | 日韩中文字幕视频在线观看 | 久久国产精品免费视频 | 欧美国产日韩一区二区三区 | 人人av在线 | 97精品国产| 综合在线一区 | 欧美综合久久 | 国产精品18hdxxxⅹ在线 | 欧美精品成人一区二区三区四区 | 色欧美在线 |