Java中如何使用Response重定向

时间:2021-05-20

直接来,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://.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重定向内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章