Redirect [ ˈriːdərekt] 重定向的意思
    image.png
    image.png
    image.png

    1. package com.itheima.web.reponse;
    2. import javax.servlet.*;
    3. import javax.servlet.http.*;
    4. import javax.servlet.annotation.*;
    5. import java.io.IOException;
    6. @WebServlet("/resp1")
    7. public class ResponseDemo1 extends HttpServlet {
    8. @Override
    9. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    10. System.out.println("resp1...");
    11. // 将resp1 重定向到resp2
    12. // 1. 设置响应状态码 302
    13. // response.setStatus(302); // 302 从重定向状态码
    14. // // 2. 设置响应头 Location 第二个参数为虚拟目录(模块名 + resp的目录)
    15. // response.setHeader("Location","/request-demo/resp2");
    16. // 简化方式完成重定向
    17. response.sendRedirect("/request-demo/resp2");
    18. }
    19. @Override
    20. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    21. this.doGet(request, response);
    22. }
    23. }