重定向
使用步骤:
1、设置状态码为302 setStatus()
2、设置响应头location setHeader()
- 更加直接的方式:response.sendRedirect("路径名");
@WebServlet("/ResponseServlet")
public class ResponseServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("demo01...");
//重定向步骤:1.设置状态码为302 2.设置响应头location 为跳转页面路径
response.setStatus(302);
response.setHeader("location","/ResponseServlet2");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
@WebServlet("/ResponseServlet2")
public class ResponseServlet2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("demo02...");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
结果展示
浏览器访问会自动跳转到 控制台输出为
浏览器抓包
网友评论