개발/Spring

[Spring] HttpServletRequest 클래스

Hide 2025. 2. 2. 23:03

HttpServletRequest란

Servlet 호출 시 생성되는 객체이다. 자바 서블릿 API의 일부로, 클라이언트가 서버에 보낸 HTTP 요청을 나타낸다. 매개변수, 헤더, 쿠키 및 세션정보와 같은 세부 정보를 캡슐화한다.

START LINE, 헤더, 바디로 구분되어 있다.

 

HttpServletRequest를 사용하면 개발자가 클라이언트에서 보낸 사용자 입력 및 데이터를 추출할 수 있다. 

 

사용예시

public void testAPI(HttpServletRequest request, HttpServletResponse response)

  String contentType = request.getHeader("Content-Type");

// contentType은 application/json 이다.

}

 

HttpServletRequest 사용 메서드

getMethod() : GET, POST 같은 메서드를 가져온다.

getRequestURI() : 요청된 URI를 가져온다.

getHeader()  요청 헤더를 가져온다.

getParameter() : 특정 요청 매개변수의 값을 검색한다.

getSession() : 요청과 관련된 HttpSession 검색