basePath:
<%
String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
也可以这样写:
<%
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ request.getContextPath() + "/";
%>
作用相当生成了以下路径:
path=WebProjectName ; (项目名称)
basePath= http://localhost:8080/WebProjectName /
好处:可以为当前的链接使用绝对路径
在我们页面跳转时,就可以写成"<%=basePath %>index.jsp"。例如:
<a href="<%=basePath %>index.jsp">首页</a>
跳转路径:http://localhost:8080/WebProjectName/index.jsp
base 标签:
设置好basePath后,在写上如下代码:
<base href="<%=basePath %>" />
这样就当前页面的默认地址为basePath,写法上如下就可以了。
<a href="index.jsp">首页</a>
跳转路径:http://localhost:8080/WebProjectName/index.jsp
ps:
request.getScheme();
返回的协议名称,默认是http。对于ssl则返回"https"
request.getServerName()
返回的是服务器域名,如果是在本地的话就是localhost
getServerPort()
获取服务器端口号