스프링 Redirect / Forward

728x90

spring 리다이렉트 and 포워드.png

1. Redirect

- 서버가 클라이언트에게 요청할 주소를 응답결과로 전달하는 것을 의미

- 클라이언트는 응답결과로 받은 요청주소를 직접 요청하게 된다.

- 브라우저가 요청하는 것이므로 주소창의 주소가 변경된다.

- Redirect는 새로운 요청이 발생하는 것이므로 HttpServletRequest 객체는 소멸 후 새롭게 생성되며 HttpSession 객체는 그대로 유지가 된다.

 

index.html

<a herf="test">Reirect</a>
@Controller
public class TestController{
	
	@GetMapping("/test")
	public String test(){
		
		return "redirect:/test2";	
	}

	@GetMapping("/test2")
	public String test(){
		
		return "test2";	
	}
}

 

test2.jsp

test2.jsp 입니다.

 

 

2. Forward

- 코드의 흐름을 서버상에서만 이동하는 것을 의미

- 브라우저는 다른 곳으로 흐름이 이동되었따는 것을 알 수 없기 때문에 주소창의 주소는 변경되지 않는다.

- HttpServletRequest, HttpSession 모두 유지가 된다.

@Controller
public class TestController{
	
	@GetMapping("/test")
	public String test(){
		
		return "forward:/test2";	
	}

	@GetMapping("/test2")
	public String test(){
		
		return "test2";	
	}
}

 

반응형

'Back-End > Spring(Boot)' 카테고리의 다른 글

스프링 Request Scope 빈 주입  (0) 2021.03.06
스프링 RequestScope  (0) 2021.03.06
스프링 Form 커맨드 태그  (0) 2021.03.05
커맨드 객체란?  (0) 2021.03.05
스프링 ViewResolver  (0) 2021.03.05