实现重定向

B 一个web资源收到客户端A请求后,B他会通知A客户端去访问另外一个web资源C,这个过程叫重定向。
常见场景:

  • 用户登录
void sendRedirect(String var1) throws IOException;
@Override
protected void doGet(HttpServletRequest req,HttpServletResponse resp) throws SeervletException,IOException{
	
	/*
		resp.setHeader("Location","/r/img");
		resp.setStatus(3o2);
	*/
	resp.sendRedirect("/img");  //重定向   要重定向的目录
}

常见面试题解析:聊聊重定向和转发的区别?
相同点:

  • 页面都会实现跳转

不同点:

  • 请求转发的时候,url不会发生变化
  • 重定向的时候,url地址栏会发生变化
posted @ 2021-12-20 15:43  萧泽  阅读(42)  评论(0)    收藏  举报