java web绝对路径的使用

来源:互联网 发布:申万宏源交易软件下载 编辑:程序博客网 时间:2024/06/14 03:38

假设我们的web服务器地址为:http://localhost:8080,web项目名称为test,那么当前我们的web应用的URLhttp://localhost:8080/test。(默认情况下)。

网页中的 “相对路径” , 它是相对于 “URL请求的地址” 去寻找资源。
比如test项目下有一个img.jsp(URL请求地址为:http://localhost:8080/test/img.jsp)和images文件夹。images文件夹下有ing.png图片文件。
当我们在img.jsp引用ing.png时可以这样:

< img src=”images/ing.png”/>

上面即为使用相对路径来引用该图片文件。

绝对路径是这样来引用该图片文件的:

< img src=”http://localhost:8080/test/images/ing.png”/>


java web中获取绝对路径的几种方式:

1、pageContext.request.contextPath

<a href="${pageContext.request.contextPath}/index.jsp">[pageContext.request.contextPath]</a>

2、使用JSTL表达式

<a href="<c:url value="index.jsp"/>">[c:url]</a>

3、使用HTML base标签

<%        String path = request.getContextPath();        String basePath = request.getScheme()+"://" +request.getServerName()+":" +request.getServerPort()+path+"/" ;        request.setAttribute("basePath", basePath);    %>    <base href="${requestScope.basePath}"><a href="index.jsp">使用base方式</a>

但凡引用地址以/开头,则以上地址都应该是相对于该路径:http://localhost:8080

原创粉丝点击