jsp页面获取绝对和相对路径

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




  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值