【JavaWeb】路径问题总结

文章目录

  • 一、相对路径和绝对路径
  • 二、前端路径问题
    • [2.1 相对路径情况分析](#2.1 相对路径情况分析)
    • [2.2 绝对路径情况分析](#2.2 绝对路径情况分析)
    • [2.3 base标签的使用](#2.3 base标签的使用)
    • [2.4 缺省项目上下文路径](#2.4 缺省项目上下文路径)
  • 三、重定向中的路径问题
    • [3.1 相对路径写法](#3.1 相对路径写法)
    • [3.2 绝对路径写法](#3.2 绝对路径写法)
  • 四、请求转发中的路径问题
    • [4.1 相对路径写法](#4.1 相对路径写法)
    • [4.2 绝对路径写法](#4.2 绝对路径写法)
    • [4.3 目标资源内相对路径处理](#4.3 目标资源内相对路径处理)

一、相对路径和绝对路径

相对路径:

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

绝对路径:

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

应用场景:

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

二、前端路径问题

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"/>
  • 寻找方式就是在 当前资源所在路径
html 复制代码
<!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"/>
  • 寻找方式就是在当前资源所在路径
html 复制代码
<!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请求转发获得
java 复制代码
@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"/>
  • 寻找方式就是在当前资源所在路径
html 复制代码
<!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),
    • 得到的正是目标资源访问的正确路径
html 复制代码
<!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),
    • 得到的正是目标资源访问的正确路径
html 复制代码
<!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请求转发获得
java 复制代码
@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),
    • 得到的正是目标资源访问的正确路径
html 复制代码
<!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 中的路径处理

html 复制代码
<!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

java 复制代码
@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
  • 寻找方式就是在当前资源所在路径
java 复制代码
@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()获取上下文路径
    • 可以将项目上下文路径定义为/缺省路径,那么路径中直接以/开头即可
java 复制代码
//绝对路径中,要写项目上下文路径
//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

java 复制代码
@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)
java 复制代码
@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
  • 在项目上下文路径为缺省 值时,也无需改变,直接以/开头即可
java 复制代码
@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中相对路径要基于该路径编写,如果使用绝对路径则不用考虑
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>
相关推荐
知识分享小能手几秒前
React学习教程,从入门到精通, React 属性(Props)语法知识点与案例详解(14)
前端·javascript·vue.js·学习·react.js·vue·react
汇能感知2 小时前
摄像头模块在运动相机中的特殊应用
经验分享·笔记·科技
阿巴Jun2 小时前
【数学】线性代数知识点总结
笔记·线性代数·矩阵
茯苓gao3 小时前
STM32G4 速度环开环,电流环闭环 IF模式建模
笔记·stm32·单片机·嵌入式硬件·学习
是誰萆微了承諾3 小时前
【golang学习笔记 gin 】1.2 redis 的使用
笔记·学习·golang
DKPT4 小时前
Java内存区域与内存溢出
java·开发语言·jvm·笔记·学习
aaaweiaaaaaa4 小时前
HTML和CSS学习
前端·css·学习·html
ST.J4 小时前
前端笔记2025
前端·javascript·css·vue.js·笔记
Suckerbin4 小时前
LAMPSecurity: CTF5靶场渗透
笔记·安全·web安全·网络安全
看海天一色听风起雨落5 小时前
Python学习之装饰器
开发语言·python·学习