- 通过jsp代码获取
request.getContextPath() :获取虚拟路径
request.getScheme() :获取请求协议
request.getServerName() :获取服务名称(IP、域名)
request.getServerPort() :获取端口号
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
out.println(basePath);
%>
- 结果为:
http://localhost:8080/
- 通过EL表达式获取
${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${request.getContextPath()}/
- 结果为:
http://localhost:8080/