最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

jsp Request獲取url信息的各種方法對(duì)比

 更新時(shí)間:2016年10月01日 11:24:29   投稿:jingxian  
下面小編就為大家?guī)硪黄猨sp Request獲取url信息的各種方法對(duì)比。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

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

假設(shè)請(qǐng)求的頁(yè)面是index.jsp,項(xiàng)目是WebDemo,則在index.jsp中獲取有關(guān)request對(duì)象的各種路徑信息如下 

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各個(gè)對(duì)應(yīng)方法所代表的含義 

從request獲取各種路徑總結(jié): 

request.getRealPath("url");//虛擬目錄映射為實(shí)際目錄 
request.getRealPath("./");//網(wǎng)頁(yè)所在的目錄 
request.getRealPath("../");//網(wǎng)頁(yè)所在目錄的上一層目錄 

假定你的web application(web應(yīng)用)名稱為news,你的瀏覽器中輸入請(qǐng)求路徑: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("/") 這個(gè)方法已經(jīng)不推薦使用了 

可以使用 

ServletContext.getRealPath(java.lang.String) instead. 
request.getSession().getServletContext().getRealPath() 得到工程文件的實(shí)際物理路徑,也就是絕對(duì)地址 
//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ù)的請(qǐng)求url,區(qū)別如下: 

1 前者返回相對(duì)路徑,后者返回完整路徑 

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

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

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

以上就是小編為大家?guī)淼膉sp Request獲取url信息的各種方法對(duì)比的全部?jī)?nèi)容了,希望對(duì)大家有所幫助,多多支持腳本之家~

相關(guān)文章

最新評(píng)論

永川市| 泰来县| 岢岚县| 陇南市| 连云港市| 北海市| 洪江市| 莱芜市| 农安县| 蒲城县| 安远县| 磴口县| 平远县| 密山市| 乌审旗| 乐昌市| 葵青区| 金寨县| 阳春市| 岑溪市| 当涂县| 开江县| 保定市| 永年县| 五原县| 赫章县| 天峻县| 浑源县| 龙海市| 隆化县| 达拉特旗| 广昌县| 沈丘县| 筠连县| 长宁县| 富裕县| 留坝县| 澄江县| 成都市| 治县。| 婺源县|