1、如图:“标签2” 拼装获取当前网页的相对路径的
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
request.getScheme() //获取当前页面使用的协议,如http
request.getServerName() //获取当前页面所在服务器的名字,如localhost,www.hesvit.com
request.getServerPort() //获取服务器的端口号,如80,8080
request.getContextPath() //获取 应用的名字 如 hesvit
pageContext.setAttribute("basePath", basePath); //pageContext是jsp内置对象,把路径设置到内置对象当中 通过${basePath} 获取
2、如图:“标签1” 获取绝对路径
<c:set var="ctx" value="${pageContext.request.contextPath}" /> //将绝对路径设置到c变量中,再用${}获取
${pageContext.request.contextPath}是JSP取得绝对路径的方法,等价于<%=request.getContextPath()%> 。
也就是取出部署的应用程序名或者是当前的项目名称
比如我的项目名称是demo1在浏览器中输入为http://localhost:8080/demo1/a.jsp ${pageContext.request.contextPath}或<%=request.getContextPath()%>取出来的就是/demo1,而"/"代表的含义就是http://localhost:8080
3、pageContext.request.contextPath和request.getContextPath()对比如下图
${}输出结果如下:
页面输出
参考: http://www.cnblogs.com/hy1988/p/5811445.html
http://www.cnblogs.com/qq3111901846/p/6242096.html