主頁 > 知識庫 > jsp Request獲取url信息的各種方法對比

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

熱門標簽:好操作的電話機器人廠家 企業(yè)怎么在聯(lián)通申請400電話 聊城智能電銷機器人外呼 地圖標注市場怎么樣 南京新思維電話機器人 南昌市地圖標注app 泰州泰興400電話 怎么申請 百度地圖添加標注圖標樣式 如何用中國地圖標注數(shù)字點

從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("./");//網(wǎng)頁所在的目錄 
request.getRealPath("../");//網(wǎng)頁所在目錄的上一層目錄 

假定你的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\ 

現(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=goExamSettype=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=goExamSettype=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信息的各種方法對比的全部內容了,希望對大家有所幫助,多多支持腳本之家~

您可能感興趣的文章:
  • jsp 使用request為頁面添加靜態(tài)數(shù)據(jù)的實例
  • JSP中Servlet的Request與Response的用法與區(qū)別
  • JSP 中request與response的用法詳解
  • JSP的request對象實例詳解
  • JSP request.setAttribute()詳解及實例
  • JSP利用過濾器解決request中文亂碼問題
  • JSP用過濾器解決request getParameter中文亂碼問題
  • Jsp中request的3個基礎實踐

標簽:開封 烏蘭察布 銅川 自貢 臨汾 吉林 白銀 山南

巨人網(wǎng)絡通訊聲明:本文標題《jsp Request獲取url信息的各種方法對比》,本文關鍵詞  jsp,Request,獲取,url,信息,;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《jsp Request獲取url信息的各種方法對比》相關的同類信息!
  • 本頁收集關于jsp Request獲取url信息的各種方法對比的相關信息資訊供網(wǎng)民參考!
  • 企业400电话

    智能AI客服机器人
    15000

    在线订购

    合计11份范本:公司章程+合伙协议+出资协议+合作协议+股权转让协议+增资扩股协议+股权激励+股东会决议+董事会决议

    推薦文章