java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Java使用Response重定向

Java中如何使用Response重定向

作者:持续更新,2天一篇

这篇文章主要介绍了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);
 }
}

浏览器访问,访问前

浏览器访问,访问后

控制台输出

从以上结果可以看出:

由于状态码固定是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重定向内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文