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

来源:互联网 发布:js设置readonly 编辑:程序博客网 时间:2024/05/16 10:45

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

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

<base href="...">是用来表明当前页面的相对路径所使用根路径的。

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

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


0 0
原创粉丝点击