Flask接口签名sign原理与实例代码浅析
283
2022-12-01
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.*;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://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可以看出,重定向不可以共享数据
重定向的特点总结:(与之对应的------请求转发详情与特点)
浏览器地址栏路径发生变化,最终地址为最后转发的地址
转发只是多次请求,转发几次就几次请求,不共享数据
可以访问其他服务器的资源
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~