服务器内部转发与客户端重定向

内部转发

image-20220402160305352

这是一次响应的时间,服务器内部经过了多少组件浏览器并不知道

下面时两个组件之间的关系

1
2
3
4
5
6
7
public class TestServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getRequestDispatcher("hi").forward(req,resp);
System.out.println("我是组件一");
}
}
1
2
3
4
5
6
public class GetServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("我是组件2");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>com.zss.servlets.TestServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/add</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>GetServlet</servlet-name>
<servlet-class>com.zss.servlets.GetServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GetServlet</servlet-name>
<url-pattern>/hi</url-pattern>
</servlet-mapping>

image-20220402161455991

成功将一个页面的处理,经过了两个组件‘

重定向

image-20220402160545843
1
2
3
4
5
6
7
public class TestServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("我是组件一");
resp.sendRedirect("hi");
}
}

当我们重定向的时候,标志状态为302,将页面重新定向到hi

image-20220402161939291