1. 客户端请求路径
1.1. 超链接跳转
当前路径:http://localhost:8080/webapp/html/hello.html1
2
3
4<!-- 目标URL:http://localhost:8080/webapp/index.html -->
<a href="http://localhost:8080/webapp/index.html">URL绝对路径,要带上虚拟目录</a>
<a href="/webapp/index.html">URI绝对路径,要带上虚拟目录</a>
<a href="../index.html">相对路径</a>
1.2. 302重定向路径
重定向是在客户端执行的,所以要带上虚拟目录getContextPath()1
resp.sendRedirect(req.getContextPath() + "/hello");
2. 服务器内部路径
总之使用绝对路径时
- 客户端请求路径,要带上虚拟目录
- 服务器内部路径,不需要虚拟目录
2.1. 服务器转发
当前路径:/aa/servlet11
2
3
4// 目标Servlet绝对路径:/hello
req.getRequestDispatcher("/hello").forward(req, resp);
// 目标Servlet相对路径:../hello
req.getRequestDispatcher("../hello").forward(req, resp);