jsp中【<%=request.getContextPath()%>】项目路径

1
2
"request.getContextPath()的值是        "<%=request.getContextPath()%><br/>
"pageContext.request.contextPath的值是   "${pageContext.request.contextPath}<br/>

  

通过运行我们发现了<%=request.getContextPath()%>和${pageContext.request.contextPath}获取的结果都是项目名(上下文),没有获取到项目的端口号

如果我们想要获取项目的绝对路径和端口号我们有什么办法呢

  

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

通过运行结果可知<%=basePath%>可知结果是

  

 完整的路径和端口号还有项目的上下文环境。

结论:项目中用<%=basePath%>来指定项目的绝对路径,可解决因为修改服务器端口号引起的找不到路径问题。

原文地址:https://www.cnblogs.com/tijun/p/7798189.html