jsp Request擷取url資訊的各種方法對比_JSP編程

來源:互聯網
上載者:User

從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資訊的各種方法對比的全部內容了,希望對大家有所協助,多多支援雲棲社區~

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.