Servlet:当前资源和目标资源之间的相对位置关系

第一种情况:

(1) 当前资源:location.html
http://localhost/Servlet_war_exploded/location.html
(2)目标资源:
http://localhost/Servlet_war_exploded/responseDemo2

相对路径可以写成:./responseDemo2
或者省略‘./’ 写成:responseDemo2
绝对路径可以写成:/Servlet_war_exploded/responseDemo2
浏览器之间访问需要加虚拟目录:/Servlet_war_exploded

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>相对和绝对路径</title>
</head>
<body>
<h1>相对路径</h1>
<h1>找到当前资源和目标资源之间的相对位置关系</h1>
<P>
    当前资源:location.html
    http://localhost/Servlet_war_exploded/location.html
</P>
<P>
    目标资源:
    http://localhost/Servlet_war_exploded/responseDemo2
</P>

<!--
    三个路径的写法都可以访问到/responseDemo2
    ./表示当前资源
-->
<a href="./responseDemo2">
    responseDemo2
</a>
<!--当是./时,可以省略./-->
<a href="responseDemo2">
    responseDemo2
</a>
<br>
<hr>
<h1>绝对路径</h1>
<a href="/Servlet_war_exploded/responseDemo2">
    responseDemo2
</a>
</body>
</html>

第二种情况:当前资源前面还有htmls文件夹,即同一父目录(虚拟目录)Servlet_war_exploded下,多了一级

(1)当前资源:location2.html
http://localhost/Servlet_war_exploded/htmls/location2.html
(2)目标资源:
http://localhost/Servlet_war_exploded/responseDemo2

相对路径可以写成: ‥/responseDemo2
绝对路径可以写成:/Servlet_war_exploded/responseDemo2

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<h1>找到当前资源和目标资源之间的相对位置关系</h1>
<P>
    当前资源:location2.html
    http://localhost/Servlet_war_exploded/htmls/location2.html
</P>
<P>
    目标资源:
    http://localhost/Servlet_war_exploded/responseDemo2

</P>
<!-- ../表示退回到上一级-->
<a href="../responseDemo2">
    responseDemo2
</a>
</body>
</html>
发布了71 篇原创文章 · 获赞 10 · 访问量 3396

猜你喜欢

转载自blog.csdn.net/JH39456194/article/details/104210931