一、Web阶段的路径
1、相对路径:不以斜杠开头的路径
demo.css
demo.js
../demo.css
2、绝对路径:以斜杠开头的路径
/day18/demo.css
/day18/demo.js
推荐使用绝对路径,因为相对路径不太稳定
例子:在a/index.html中引入一副图片
< img src=”1.jpg”>
第一次访问index.html的访问路径
http://localhost/test/a/index.html
相对路径是相对于当前文件(index.html)所在的路径是:
http://localhost/test/a
再拼接上相对路径(1.jpg),就得出了图片的完整路径:
http://localhost/text/a/1.jpg
第二次访问,index.html的访问路径
http://localhost/test/HelloServlet
相对路径是相对于当前文件(index.html)所在的路径是:
http://localhost/test/
再拼接上相对路径(1.jpg),就得出了图片的完整路径:
http://localhost/text/1.jpg
在Web阶段推荐使用绝对路径,在写绝对路径时,什么时候需要加上当前Web应用的虚拟路径,什么时候不需要加?
(1)请求转发:request.getRequestDispatcher(“/a/index.html”).forward(res,req);
(2)请求包含:request.getRequestDispatcher(“/a/index.html”).include(res,req);
<%@include file=”/a/index.jsp” %>
<%@page errorPage=”/a/index.jsp”%>
请求转发和包含因为只能转发或包含同一个Web应用内部的资源,所以在写路径时,不需要加上Web应用的虚拟路径!!
下面的都需要
(3)请求重定向:response.sendRedirect(“/test/a/index.html”);
(4)定时刷新:response.setHeader(“Refresh”,”3;url=/test/a/index.html”);
(5)图片的路径
< img src=”test/a/1.jpg”>
(6)引入JS文件
< script src=”/test/a/1.js”>< /script>
(7)引入CSS文件
< link href=”/test/a/1.css”/>
(8)表单的提交路径
< form action=”/test/HelloServlet”>< /form>
(9)超链接跳转的路径
< a href=”/test/a/index.html”>< /a>