获取页面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  
原文地址:https://www.cnblogs.com/JealousGirl/p/jUrl.html