首页 > 编程语言 > Java中如何使用Response重定向
2020
10-08

Java中如何使用Response重定向

直接来,ResponseDemo1代码,get请求到post请求,重定向到ResponseDemo2

我的虚拟目录为

ResponseDemo1代码,302是重定向状态码,Http状态码大全

package com.lingaolu.response;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
 
/**
 * @author 林高禄
 * @create 2020-07-10-15:24
 */
@WebServlet("/responseDemo1")
public class ResponseDemo1 extends HttpServlet {
 @Override
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  System.out.println("responseDemo1进来了......");
  // 设置状态码为302
  response.setStatus(302);
  // 动态获取虚拟目录,以后修改虚拟目录的时候就不用了修改代码
  String contextPath = request.getContextPath();
  // 设置响应头location
  response.setHeader("location",contextPath+"/responseDemo2");
 }
 
 @Override
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  this.doPost(request,response);
 }
}

ResponseDemo2代码

package com.lingaolu.response;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
 
/**
 * @author 林高禄
 * @create 2020-07-10-15:24
 */
@WebServlet("/responseDemo2")
public class ResponseDemo2 extends HttpServlet {
 @Override
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  System.out.println("responseDemo2进来了......");
 }
 
 @Override
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  this.doPost(request,response);
 }
}

浏览器访问,访问前

浏览器访问,访问后

控制台输出

从以上结果可以看出:

  • 浏览器上的请求地址变了
  • 重定向是2次请求,一次是responseDemo1请求,一次是responseDemo2请求

由于状态码固定是302,响应头固定是location,所以请求转发提供了更方便的方式

void sendRedirect(String var1)

ResponseDemo3代码

package com.lingaolu.response;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
 
/**
 * @author 林高禄
 * @create 2020-07-10-15:24
 */
@WebServlet("/responseDemo3")
public class ResponseDemo3 extends HttpServlet {
 @Override
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  System.out.println("responseDemo3进来了......");;
  // 动态获取虚拟目录,以后修改虚拟目录的时候就不用了修改代码
  String contextPath = request.getContextPath();
  response.sendRedirect(contextPath+"/responseDemo2");
 
 }
 
 @Override
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  this.doPost(request,response);
 }
}

启动,浏览器访问

控制台输出

我们把ResponseDemo3代码改一下,重定向到百度

package com.lingaolu.response;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
 
/**
 * @author 林高禄
 * @create 2020-07-10-15:24
 */
@WebServlet("/responseDemo3")
public class ResponseDemo3 extends HttpServlet {
 @Override
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  System.out.println("responseDemo3进来了......");;
  response.sendRedirect("http://www.baidu.com");
 
 }
 
 @Override
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  this.doPost(request,response);
 }
}

重启访问,就重定向到百度了

从上面结果我们可以看出,重定向可以访问其他服务器的资源

我们写一个ResponseDemo4,探索一下共享数据情况,ResponseDemo4里请求写进了一个数据name

package com.lingaolu.response;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
 
/**
 * @author 林高禄
 * @create 2020-07-10-15:24
 */
@WebServlet("/responseDemo4")
public class ResponseDemo4 extends HttpServlet {
 @Override
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  System.out.println("responseDemo4进来了......");
  request.setAttribute("name","林大帅");
  String contextPath = request.getContextPath();
  response.sendRedirect(contextPath+"/responseDemo2");
 
 }
 
 @Override
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  this.doPost(request,response);
 }
}

同时ResponseDemo2改一下,获取数据name

重启,浏览器访问

控制台输出:

从上面的null可以看出,重定向不可以共享数据

重定向的特点总结:(与之对应的------请求转发详情与特点

  • 浏览器地址栏路径发生变化,最终地址为最后转发的地址
  • 转发只是多次请求,转发几次就几次请求,不共享数据
  • 可以访问其他服务器的资源

到此这篇关于Java中如何使用Response重定向的文章就介绍到这了,更多相关Java使用Response重定向内容请搜索自学编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持自学编程网!

编程技巧