獲取頁面URL兩種方式


以請求http://localhost:8080/doctor/demo?code=1為例


一:用java代碼獲取

 1     //獲取URL中的請求參數。即?后的條件    code=1
 2     String queryString = request.getQueryString() ;
 3 
 4   //獲取URI。 /doctor/demo
 5   String requestURI = request.getRequestURI() ;
 6 
 7   //獲取URL(不帶請求參數)。即協議+ip地址+端口號+請求方法    http://localhost:8080/doctor/demo
 8   StringBuffer requestURL = request.getRequestURL() ;
 9 
10   //返回調用servlet請求的URL部分。/doctor/demo
11   String servletPath = request.getServletPath() ;
12 
13 
14   //獲取ip地址。    localhost
15   String serverName = request.getServerName();
16 
17   //獲取請求協議    http
18   String scheme = request.getScheme();
19 
20   //獲取端口號    8080
21   int serverPort = request.getServerPort();

 

二:在頁面中獲取
  

 1     //設置或獲取對象指定的文件名或路徑。    /doctor/demo
 2   window.location.pathname;
 3 
 4   //設置或獲取整個 URL 為字符串。http://localhost:8080/doctor/demo?code=1
 5   window.location.href;
 6 
 7   //設置或獲取與 URL 關聯的端口號碼。8080
 8   window.location.prot;
 9 
10   //設置或獲取 URL 的協議部分。http:
11   window.location.protocol;
12 
13   //設置或獲取 location 或 URL 的 hostname 和 port 號碼。localhost:8080
14   window.location.host;
15 
16   //設置或獲取 href 屬性中跟在問號后面的部分。?code=1
17   window.location.search;
18 
19  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM