Redirect [ ˈriːdərekt] 重定向的意思
package com.itheima.web.reponse;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet("/resp1")
public class ResponseDemo1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("resp1...");
// 将resp1 重定向到resp2
// 1. 设置响应状态码 302
// response.setStatus(302); // 302 从重定向状态码
// // 2. 设置响应头 Location 第二个参数为虚拟目录(模块名 + resp的目录)
// response.setHeader("Location","/request-demo/resp2");
// 简化方式完成重定向
response.sendRedirect("/request-demo/resp2");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}