相对路径:
/
开头file
协议下,使用的是磁盘路径http
协议下,使用的是url路径./
表示当前资源所在路径,可以省略不写../
表示当前资源所在路径的上一层路径,需要时要手动添加绝对路径:
/
开头应用场景:
相对路径情况1: web/index.html 中引入 web/static/img/logo.png
http://localhost:8080/web03_war_exploded/index.html
index.html
http://localhost:8080/web03_war_exploded/
http://localhost:8080/web03_war_exploded/static/img/logo.png
<img src="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
http://localhost:8080/web03_war_exploded/a/b/c/test.html
test.html
http://localhost:8080/web03_war_exploded/a/b/c/
http://localhost:8080/web03_war_exploded/static/img/logo.png
<img src="../../../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
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);
}
}
http://localhost:8080/web03_war_exploded/view1Servlet
view1Servlet
http://localhost:8080/web03_war_exploded/
http://localhost:8080/web03_war_exploded/static/img/logo.png
<img src="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>
绝对路径情况1: web/index.html 中引入 web/static/img/logo.png
http://localhost:8080/web03_war_exploded/index.html
http://localhost:8080
http://localhost:8080/web03_war_exploded/static/img/logo.png
<img 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
http://localhost:8080/web03_war_exploded/a/b/c/test.html
http://localhost:8080
http://localhost:8080/web03_war_exploded/static/img/logo.png
<img 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
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);
}
}
http://localhost:8080/web03_war_exploded/view1Servlet
http://localhost:8080
http://localhost:8080/web03_war_exploded/static/img/logo.png
<img 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>
base标签定义页面相对路径公共前缀
head标签
中,用于定义相对路径的公共前缀./
或者../
修饰,则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>
项目上下文路径变化问题
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 {
}
}
http://localhost:8080/web03_war_exploded/x/y/z/servletA
servletA
http://localhost:8080/web03_war_exploded/x/x/z/
http://localhost:8080/web03_war_exploded/a/b/c/test.html
../../../a/b/c/test/html
(http://localhost:8080/web03_war_exploded/x/y/z/)
(../../../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");
}
}
访问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 {
}
}
http://localhost:8080/web03_war_exploded/x/y/servletB
servletB
http://localhost:8080/web03_war_exploded/x/x/
http://localhost:8080/web03_war_exploded/a/b/c/test.html
../../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
)../
抵消一层目录,正好是目标资源正常获取的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);
}
}
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);
}
}
此时需要注意,请求转发是服务器行为,浏览器不知道,地址栏不变化,
http://localhost:8080/web03_war_exploded/x/y/servletB
那么此时 test.html资源的所在路径就是
http://localhost:8080/web03_war_exploded/x/y/
<!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>