【JavaWeb】路径问题总结

6 篇文章 0 订阅


一、相对路径和绝对路径

相对路径:

  • 相对路径的规则是:
    • 以当前资源所在的路径为出发点去寻找目标资源
  • 相对路径不以 / 开头
  • file协议下,使用的是磁盘路径
  • http协议下,使用的是url路径
  • 相对路径中可以使用 ./表示当前资源所在路径,可以省略不写
  • 相对路径中可以使用../表示当前资源所在路径的上一层路径,需要时要手动添加

绝对路径:

  • 绝对路径的规则是:
    • 使用以一个固定的路径做出出发点去寻找目标资源,和当前资源所在的路径没有关系
  • 绝对路径要以/ 开头
  • 绝对路径的写法中,不以当前资源的所在路径为出发点,所以不会出现 ./ 和…/
  • 不同的项目和不同的协议下,绝对路径的基础位置可能不同,要通过测试确定
  • 绝对路径的好处就是:无论当前资源位置在哪,寻找目标资源路径的写法都一致

应用场景:

  1. 前端代码中,href src action 等属性
  2. 请求转发和重定向中的路径

二、前端路径问题

1

2.1 相对路径情况分析

相对路径情况1: web/index.html 中引入 web/static/img/logo.png

  • 访问 index.htmlurl 为 :
    • http://localhost:8080/web03_war_exploded/index.html
  • 当前资源为 :
    • index.html
  • 当前资源的所在路径为 :
    • http://localhost:8080/web03_war_exploded/
  • 要获取的目标资源url为 :
    • http://localhost:8080/web03_war_exploded/static/img/logo.png
  • index.html中定义的了 :
    • <img src="static/img/logo.png"/>
  • 寻找方式就是在 当前资源所在路径
    • (http://localhost:8080/web03_war_exploded/)
    • 后拼接src属性值
    • (static/img/logo.png),
    • 正好是目标资源正常获取的
    • url(http://localhost:8080/web03_war_exploded/static/img/logo.png)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    
    <img src="static/img/logo.png">
</body>
</html>

相对路径情况2 : web/a/b/c/test.html 中引入 web/static/img/logo.png

  • 访问test.html的url为 :
    • http://localhost:8080/web03_war_exploded/a/b/c/test.html
  • 当前资源为 :
    • test.html
  • 当前资源的所在路径为 :
    • http://localhost:8080/web03_war_exploded/a/b/c/
  • 要获取的目标资源url为 :
    • http://localhost:8080/web03_war_exploded/static/img/logo.png
  • test.html中定义的了 :
    • <img src="../../../static/img/logo.png"/>
  • 寻找方式就是在当前资源所在路径
    • (http://localhost:8080/web03_war_exploded/a/b/c/)
    • 后拼接src属性值
    • (…/…/…/static/img/logo.png),
    • 其中 …/可以抵消一层路径,正好是目标资源正常获取的
    • url(http://localhost:8080/web03_war_exploded/static/img/logo.png)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <!-- ../代表上一层路径 -->
    <img src="../../../static/img/logo.png">
</body>
</html>

相对路径情况3 : web/WEB-INF/views/view1.html 中引入 web/static/img/logo.png

  • view1.html在WEB-INF下,需要通过Servlet请求转发获得
@WebServlet("/view1Servlet")
public class View1Servlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        RequestDispatcher requestDispatcher = req.getRequestDispatcher("WEB-INF/views/view1.html");
        requestDispatcher.forward(req,resp);
    }
}
  • 访问 view1.html 的 url 为 :
    • http://localhost:8080/web03_war_exploded/view1Servlet
  • 当前资源为 :
    • view1Servlet
  • 当前资源的所在路径为 :
    • http://localhost:8080/web03_war_exploded/
  • 要获取的目标资源url为 :
    • http://localhost:8080/web03_war_exploded/static/img/logo.png
  • view1.html中定义的了 :
    • <img src="static/img/logo.png"/>
  • 寻找方式就是在当前资源所在路径
    • (http://localhost:8080/web03_war_exploded/)
    • 后拼接src属性值(static/img/logo.png),
    • 正好是目标资源正常获取的
    • url(http://localhost:8080/web03_war_exploded/static/img/logo.png)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<img src="static/img/logo.png">
</body>
</html>

2.2 绝对路径情况分析

绝对路径情况1: web/index.html 中引入 web/static/img/logo.png

  • 访问index.html的 url 为 :
    • http://localhost:8080/web03_war_exploded/index.html
  • 绝对路径的基准路径为 :
    • http://localhost:8080
  • 要获取的目标资源url为 :
    • http://localhost:8080/web03_war_exploded/static/img/logo.png
  • index.html中定义的了 :
    • <img src="/web03_war_exploded/static/img/logo.png"/>
  • 寻找方式就是在基准路径
    • (http://localhost:8080)
    • 后面拼接src属性值
    • (/web03_war_exploded/static/img/logo.png),
    • 得到的正是目标资源访问的正确路径
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <!-- 绝对路径写法 -->
    <img src="/web03_war_exploded/static/img/logo.png">
</body>
</html>

绝对路径情况2: web/a/b/c/test.html 中引入 web/static/img/logo.png

  • 访问test.html的url为 :
    • http://localhost:8080/web03_war_exploded/a/b/c/test.html
  • 绝对路径的基准路径为 :
    • http://localhost:8080
  • 要获取的目标资源url为 :
    • http://localhost:8080/web03_war_exploded/static/img/logo.png
  • test.html中定义的了 :
    • <img src="/web03_war_exploded/static/img/logo.png"/>
  • 寻找方式就是在基准路径
    • (http://localhost:8080)
    • 后面拼接src属性值
    • (/web03_war_exploded/static/img/logo.png),
    • 得到的正是目标资源访问的正确路径
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <!-- 绝对路径写法 -->
    <img src="/web03_war_exploded/static/img/logo.png">
</body>
</html>

绝对路径情况3: web/WEB-INF/views/view1.html 中引入 web/static/img/logo.png

  • view1.html 在 WEB-INF 下,需要通过Servlet请求转发获得
@WebServlet("/view1Servlet")
public class View1Servlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        RequestDispatcher requestDispatcher = req.getRequestDispatcher("WEB-INF/views/view1.html");
        requestDispatcher.forward(req,resp);
    }
}
  • 访问view1.html的url为 :
    • http://localhost:8080/web03_war_exploded/view1Servlet
  • 绝对路径的基准路径为 :
    • http://localhost:8080
  • 要获取的目标资源url为 :
    • http://localhost:8080/web03_war_exploded/static/img/logo.png
  • view1.html中定义的了 :
    • <img src="/web03_war_exploded/static/img/logo.png"/>
  • 寻找方式就是在基准路径
    • (http://localhost:8080)
    • 后面拼接src属性值
    • (/static/img/logo.png),
    • 得到的正是目标资源访问的正确路径
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
	<img src="/web03_war_exploded/static/img/logo.png">
</body>
</html>

2.3 base标签的使用

base标签定义页面相对路径公共前缀

  • base 标签定义在head标签中,用于定义相对路径的公共前缀
  • base 标签定义的公共前缀只在相对路径上有效,绝对路径中无效
  • 如果相对路径开头有./或者../修饰,则base标签对该路径同样无效

index.html 和a/b/c/test.html 以及view1Servlet 中的路径处理

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!--定义相对路径的公共前缀,将相对路径转化成了绝对路径-->
    <base href="/web03_war_exploded/">
</head>
<body>
    <img src="static/img/logo.png">
</body>
</html>

2.4 缺省项目上下文路径

项目上下文路径变化问题

  • 通过 base标签虽然解决了相对路径转绝对路径问题,但是base中定义的是项目的上下文路径
  • 项目的上下文路径是可以随意变化的
  • 一旦项目的上下文路径发生变化,所有base标签中的路径都需要改

解决方案

  • 项目的上下文路径进行缺省设置,设置为 /,
  • 所有的绝对路径中就不必填写项目的上下文了,直接就是/开头即可

三、重定向中的路径问题

目标 :由/x/y/z/servletA重定向到a/b/c/test.html

@WebServlet("/x/y/z/servletA")
public class ServletA extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        
    }
}

3.1 相对路径写法

  • 访问ServletA的url为 :
    • http://localhost:8080/web03_war_exploded/x/y/z/servletA
  • 当前资源为 :
    • servletA
  • 当前资源的所在路径为 :
    • http://localhost:8080/web03_war_exploded/x/x/z/
  • 要获取的目标资源url为 :
    • http://localhost:8080/web03_war_exploded/a/b/c/test.html
  • ServletA重定向的路径 :
    • ../../../a/b/c/test/html
  • 寻找方式就是在当前资源所在路径
    • (http://localhost:8080/web03_war_exploded/x/y/z/)
    • 后拼接
      -(../../../a/b/c/test/html),
    • 形成
    • (http://localhost:8080/web03_war_exploded/x/y/z/…/…/…/a/b/c/test/html)
    • 每个…/抵消一层目录,正好是目标资源正常获取的
    • url(http://localhost:8080/web03_war_exploded/a/b/c/test/html)
@WebServlet("/x/y/z/servletA")
public class ServletA extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 相对路径重定向到test.html
        resp.sendRedirect("../../../a/b/c/test.html");
    }
}

3.2 绝对路径写法

  • 访问ServletA的url为 :

    • http://localhost:8080/web03_war_exploded/x/y/z/servletA
  • 绝对路径的基准路径为 :

    • http://localhost:8080
  • 要获取的目标资源url为 :

    • http://localhost:8080/web03_war_exploded/a/b/c/test.html
  • ServletA重定向的路径 :

    • /web03_war_exploded/a/b/c/test.html
  • 寻找方式就是在基准路径
    - (http://localhost:8080)

    • 后面拼接
      - (/web03_war_exploded/a/b/c/test.html),
    • 得到
    • ( http://localhost:8080/web03_war_exploded/a/b/c/test.html)
    • 正是目标资源访问的正确路径
  • 绝对路径中需要填写项目上下文路径,但是上下文路径是变换的

    • 可以通过 ServletContext的getContextPath()获取上下文路径
    • 可以将项目上下文路径定义为/缺省路径,那么路径中直接以/开头即可
//绝对路径中,要写项目上下文路径
//resp.sendRedirect("/web03_war_exploded/a/b/c/test.html");
// 通过ServletContext对象动态获取项目上下文路径
//resp.sendRedirect(getServletContext().getContextPath()+"/a/b/c/test.html");
// 缺省项目上下文路径时,直接以/开头即可
resp.sendRedirect("/a/b/c/test.html");

四、请求转发中的路径问题

目标 :由x/y/servletB请求转发到a/b/c/test.html

@WebServlet("/x/y/servletB")
public class ServletB extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}

4.1 相对路径写法

  • 访问ServletB的url为 :
    • http://localhost:8080/web03_war_exploded/x/y/servletB
  • 当前资源为 :
    • servletB
  • 当前资源的所在路径为 :
    • http://localhost:8080/web03_war_exploded/x/x/
  • 要获取的目标资源url为 :
    • http://localhost:8080/web03_war_exploded/a/b/c/test.html
  • ServletA请求转发路径 :
    • ../../a/b/c/test/html
  • 寻找方式就是在当前资源所在路径
    • (http://localhost:8080/web03_war_exploded/x/y/)
    • 后拼接
    • (../../a/b/c/test/html),
    • 形成
    • (http://localhost:8080/web03_war_exploded/x/y/../../a/b/c/test/html)
    • 每个../抵消一层目录,正好是目标资源正常获取的
    • url(http://localhost:8080/web03_war_exploded/a/b/c/test/html)
@WebServlet("/x/y/servletB")
public class ServletB extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        RequestDispatcher requestDispatcher = req.getRequestDispatcher("../../a/b/c/test.html");
        requestDispatcher.forward(req,resp);
    }
}

4.2 绝对路径写法

  • 请求转发只能转发到项目内部的资源,其绝对路径无需添加项目上下文路径
  • 请求转发绝对路径的基准路径相当于http://localhost:8080/web03_war_exploded
  • 在项目上下文路径为缺省值时,也无需改变,直接以/开头即可
@WebServlet("/x/y/servletB")
public class ServletB extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        RequestDispatcher requestDispatcher = req.getRequestDispatcher("/a/b/c/test.html");
        requestDispatcher.forward(req,resp);
    }
}

4.3 目标资源内相对路径处理

  • 此时需要注意,请求转发服务器行为,浏览器不知道,地址栏不变化,

    • 相当于我们访问test.html的路径为
    • http://localhost:8080/web03_war_exploded/x/y/servletB
  • 那么此时 test.html资源的所在路径就是

    • http://localhost:8080/web03_war_exploded/x/y/
    • 所以test.html中相对路径要基于该路径编写,如果使用绝对路径则不用考虑
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <!--
		当前资源路径是     http://localhost:8080/web03_war_exploded/x/y/servletB
        当前资源所在路径是  http://localhost:8080/web03_war_exploded/x/y/
        目标资源路径=所在资源路径+src属性值 
		http://localhost:8080/web03_war_exploded/x/y/../../static/img/logo.png
        http://localhost:8080/web03_war_exploded/static/img/logo.png
		得到目标路径正是目标资源的访问路径	
    -->
<img src="../../static/img/logo.png">
</body>
</html>
  • 27
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

加文格罗夫斯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值