[Spring] HttpServletRequest과 제공 메서드

2025. 6. 20. 23:20·Spring/MVC

이전 포스팅에서 언급 했듯이, 서블릿은 개발자가 HTTP 요청 메시지를 편리하게 사용할 수 있도록 개발자 대신 HTTP 요청 메시지를 파싱하여 이를 파라미터로 제공한다.

HttpServletRequest은 HTTP 요청 메시지의 모든 내용을 편리하게 조회하고 사용할 수 있다.

또한, Arrtibute 기능도 지원하여 Map 형태의 임시 저장소 기능도 수행한다.

HttpServletRequest이 어떤 메서드로 어떤 기능을 제공하는지 살펴보자


HttpServletRequest 이 제공하는 기능

start line 정보

Http 메서드, URL, 쿼리 파라미터, 프로토콜 등을 조회할 수 있다.

private static void startLine(HttpServletRequest request) {
        System.out.println("--- REQUEST-LINE - start ---");
        System.out.println("request.getMethod() = " + request.getMethod()); //GET
        System.out.println("request.getProtocol() = " + request.getProtocol()); //HTTP/1.1
        System.out.println("request.getScheme() = " + request.getScheme()); //http
        // http://localhost:8080/request-header
        System.out.println("request.getRequestURL() = " + request.getRequestURL());
        // /request-header
        System.out.println("request.getRequestURI() = " + request.getRequestURI());
        //username=hi
        System.out.println("request.getQueryString() = " +
                request.getQueryString());
        System.out.println("request.isSecure() = " + request.isSecure()); //https 사용유무
        System.out.println("--- REQUEST-LINE - end ---");
    }

 

Header 정보

모든 헤더 정보에 대해 조회할 수 있다.

private static void headers(HttpServletRequest request) {
        System.out.println("--- Headers - start ---");
        request.getHeaderNames().asIterator()
                       .forEachRemaining(headerName -> System.out.println(headerName + " : "
                               + request.getHeader(headerName)));

        System.out.println("--- Headers - end ---");
        System.out.println();
    }

이외에도 getXXX() 메서드를 통해 다양한 헤더를 편리하게 조회할 수 있다.

private static void headerUtils(HttpServletRequest request) {
        System.out.println("--- Header 편의 조회 start ---");
        System.out.println("[Host 편의 조회]");
        System.out.println("request.getServerName() = " +
                request.getServerName()); //Host 헤더
        System.out.println("request.getServerPort() = " +
                request.getServerPort()); //Host 헤더
        System.out.println();
        System.out.println("[Accept-Language 편의 조회]");
        request.getLocales().asIterator()
                .forEachRemaining(locale -> System.out.println("locale = " +
                        locale));
        System.out.println("request.getLocale() = " + request.getLocale());
        System.out.println();
        System.out.println("[cookie 편의 조회]");
        if (request.getCookies() != null) {
            for (Cookie cookie : request.getCookies()) {
                System.out.println(cookie.getName() + ": " + cookie.getValue());
            }
        }
        System.out.println();
        System.out.println("[Content 편의 조회]");
        System.out.println("request.getContentType() = " +
                request.getContentType());
        System.out.println("request.getContentLength() = " +
                request.getContentLength());
        System.out.println("request.getCharacterEncoding() = " +
                request.getCharacterEncoding());
        System.out.println("--- Header 편의 조회 end ---");
        System.out.println();
    }

 

기타 정보

기타 정보란 HTTP 메시지의 정보는 아니지만, 리모트나 로케일 같은 기타 정보들도 필요한 내용들을 getXXX() 메서드를 통해 쉽게 조회할 수 있다.

 

다음 포스팅은 HTTP 요청 메시지의 데이터를 어떻게 조회하는지 알아보겠다 !

'Spring > MVC' 카테고리의 다른 글

[Spring] HTTP Response 데이터 처리하기  (0) 2025.06.21
[Spring] HttpServletResponse와 제공 메서드  (0) 2025.06.21
[Spring] HTTP Request 데이터를 처리하는 3가지 방법  (0) 2025.06.21
[Spring] Servlet - 서블릿이란?  (0) 2025.06.20
[Spring] Web Server, Web Application Server(WAS)  (0) 2025.06.20
'Spring/MVC' 카테고리의 다른 글
  • [Spring] HttpServletResponse와 제공 메서드
  • [Spring] HTTP Request 데이터를 처리하는 3가지 방법
  • [Spring] Servlet - 서블릿이란?
  • [Spring] Web Server, Web Application Server(WAS)
xuv2
xuv2
집에 가고 싶다
  • xuv2
    xuvlog
    xuv2
  • 전체
    오늘
    어제
    • 전체 글 모아보기 (171) N
      • 잡담 (9)
      • 도전 , 자격증 (2)
      • Error (5)
      • Java (23)
      • Spring (40) N
        • Core (10)
        • MVC (21) N
        • Thymeleaf (9)
      • DataBase (6)
        • Database Modeling (4)
        • SQL (2)
      • HTTP (11)
      • Network (17)
      • Software Engineering (3)
      • Operating System (3)
      • Algorithm (16)
      • Project (18)
        • Web (9)
        • iOS (8)
        • Python (1)
      • A.I (13)
      • Linux (5)
  • 블로그 메뉴

    • 홈
  • 링크

    • Github
  • 인기 글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
xuv2
[Spring] HttpServletRequest과 제공 메서드
상단으로

티스토리툴바