Javaweb request forwarding and redirection implementation details

Request forwarding: When the same controller forwards the request to another request mapping, the request address will not change

 //请求转发
  @RequestMapping("/testFoeward")
  //@ResponseBody
  public String testforWard1() {
  	System.out.println("testforWard1执行了");
  	return"forward:/test";//	请求转发到/test
  	
  }
  @RequestMapping("/test")
  public String testforWard2() {
  	System.out.println("testforward2执行了");
  	return"hello";//跳转到hello.jsp
  	
  }


Redirection: redirect the request to a different controller

  //重定向
	/*
	 * (1)可以从当前controller中的方法重定向到另一个controller方法
	 * [return " redirect:/资源路径"]
	 * 请求转发路径会发生改变
	 */
  @RequestMapping("/testRedirect")
  public String testredirect1() {
  	System.out.println("testRedirect执行了");
  	return "redirect:http://localhost:8080/day_22/test";
      //return "redirect:http://www.baidu.com";重定向到百度
  }

Some high-frequency interview questions collected in the latest 2020 (all organized into documents), there are many dry goods, including mysql, netty, spring, thread, spring cloud, jvm, source code, algorithm and other detailed explanations, as well as detailed learning plans, interviews Question sorting, etc. For those who need to obtain these contents, please add Q like: 11604713672

Guess you like

Origin blog.csdn.net/weixin_51495453/article/details/113407986