首页 > 其他 > 详细

request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/"

时间:2019-05-11 15:38:37      阅读:126      评论:0      收藏:0      [点我收藏+]

String path = request.getContextPath(); 
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
    <base href=" <%=basePath%>"> 

这个语句是用来拼装当前网页的相对路径的。 

<base href="...">是用来表明当前页面的相对路径所使用的根路径的。 
比如,页面内部有一个连接,完整的路径应该是 http://localhost:80/myblog/authen/login.do 
其中http://server/是服务器的基本路径,myblog是当前应用程序的名字,那么,我的根路径应该是那么http://localhost:80/myblog/。 

有了这个 <base ... >以后,我的页面内容的连接,我不想写全路径,我只要写 authen/login.do就可以了。服务器会自动把 <base ...>指定的路径和页面内的相对路径拼装起来,组成完整路径。 
如果没有这个 <base...>,那么我页面的连链接就必须写全路径,否则服务器会找不到。 

request.getSchema()可以返回当前页面使用的协议,就是上面例子中的“http” 
request.getServerName()可以返回当前页面所在的服务器的名字,就是上面例子中的“localhost" 
request.getServerPort()可以返回当前页面所在的服务器使用的端口,就是80, 
request.getContextPath()可以返回当前页面所在的应用的名字,就是上面例子中的myblog 
这四个拼装起来,就是当前应用的跟路径了

request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/"

原文:https://www.cnblogs.com/yunhemeihe/p/10848703.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!