
HttpServletRequest 생성 시점
웹클라이언트(웹브라우저)로부터 서버로 요청이 들어오면
서버에서는 HttpServletRequest를 생성하여, 요청정보에 들어있는 path로 매핑된 서블릿에게 전달한다
(응답을 보낼 때 사용하기 위해 HttpServletResponse 객체도 생성해 함께 전달)
HttpServletRequest 사용 이유
http 프로토콜의 request정보를 서블릿에게 전달하기 위해서 사용한다.
헤더정보, 파라미터, 쿠키 등의 정보를 읽을 수 있는 메소드를 가지고 있다.
Body의 Stream을 읽어들이는 메소드를 가지고 있다.
HttpServletResponse 사용 이유
요청을 보낸 클라이언트에게 content type,응답메세지 등 응답에 관련된 정보를 보내기 위해서 사용한다.
HttpServletRequest 사용 방법
http://localhost:8080/param?name=kim
위의 url을 서버로 보낸다고 하면
@RequestMapping("/param")
public String list(HttpServletRequest request, HttpServletResponse response){
String requestName = request.getParameter("name"); //request로 넘어온 name
response.setStatus(HttpServletResponse.SC_OK); //응답 상태 설정
return "/list";
}
url로 넘어온 name이 requestName에 저장되고
응답상태(SC_OK=성공)를 response에 설정하는 예시이다.
'web > spring' 카테고리의 다른 글
| Spring JPA - 페치조인 최적화 (0) | 2024.03.23 |
|---|---|
| Spring Security (스프링 시큐리티) OAuth2 정리 (0) | 2024.03.07 |
| Cascade, OrphanRemoval 정리 (0) | 2024.02.23 |
| Spring Security (3) - Security Filter Chain (1) | 2024.02.03 |
| Spring Security(2) - 주요 모듈 (0) | 2024.02.03 |