Response_重定向的代码实现以及特点
重定向:资源跳转的方式
@WebServlet(name = "ResponseDemo1", value = "/ResponseDemo1") public class ResponseDemo1 extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("demo1..."); //访问/ResponseDemo1,会自动跳转到/ResponseDemo2资源 //1.设置状态码为302 // response.setStatus(302); // //2.设置响应头location // response.setHeader("location", "/ResponseDemo2"); //简单的重定向 response.sendRedirect("/ResponseDemo2"); } }
@WebServlet(name = "ResponseDemo2", value = "/ResponseDemo2") public class ResponseDemo2 extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("demo222222..."); } }
重定向特点:
1.地址栏发送变化
2.重定向可以访问其他站点(服务器)的资源
3.重定向是两次请求
转发的特点:
1.转发地址栏路径不变
2.转发只能访问当前服务器下的资源
3.转发只是一次请求
标签:重定向,ResponseDemo1,代码,ResponseDemo2,request,HttpServletResponse,Response,response From: https://www.cnblogs.com/qihaokuan/p/16960591.html