package top.qaqaq.P150;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author RichieZhang
* @create 2022-12-05 下午 6:09
*/
public class ForwardC extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// super.doGet(req, resp);
System.out.println("经过了ForwardC程序");
req.getRequestDispatcher("/a/b/c.html").forward(req,resp);
}
}
<servlet>
<servlet-name>ForwardC</servlet-name>
<servlet-class>top.qaqaq.P150.ForwardC</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ForwardC</servlet-name>
<url-pattern>/forwardC</url-pattern>
</servlet-mapping>
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
这是web下的index.html <br/>
<a href="a/b/c.html">a/b/c.html</a><br/>
<a href="http://localhost:8080/JavaWeb_Code_Servlet/forwardC">请求转发:a/b/c.html</a>
<a href="/">斜杠</a>
</body>
</html>
/a/b/c.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!-- base标签设置页面相对路径工作时参照的地址
href 属性就是参照的地址值
-->
<base href="http://localhost:8080/JavaWeb_Code_Servlet/a/b/">
</head>
<body>
这是a下的b下的c.html页面 <br/>
<a href="../../index.html">跳回首页</a><br/>
</body>
</html>