[스프링 MVC 1편] - 서블릿 HttpServletRequest(startLine, header)

2023. 1. 2. 15:23·Spring
목차
  1. HttpServletRequest
  2. HttpServletRequest 역할
  3. HttpServletRequest - 기본 사용법

HttpServletRequest

HttpServletRequest 역할

  • 웹브라우저에서 들어온 Http요청메시지를 직접 파싱하기는 너무 반복적인 귀찮은 일이므로, 개발자대신 서블릿이 대신 파싱하여 HttpServletRequest객체에 담아 제공한다.
  • HTTP 요청 메시지에는 다음과같은 정보가 담겨있다.

START LINE

  • HTTP 메소드
  • URL
  • 쿼리 스트링
  • 스키마, 프로토콜

헤더

  • 헤더 조회

바디

  • form 파라미터 형식 조회
  • message body 데이터 직접 조회

 

HttpServletRequest - 기본 사용법

start-line 정보

private void printStartLine(HttpServletRequest request) {
    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
    System.out.println("request.getRequestURL() = " + request.getRequestURL());
    System.out.println("request.getRequestURI() = " + request.getRequestURI());
    System.out.println("request.getQueryString() = " + request.getQueryString());
    System.out.println("request.isSecure() = " + request.isSecure()); //https 사용 유무
}

헤더 정보

//Header 모든 정보
private void printHeaders(HttpServletRequest request) {
    request.getHeaderNames().asIterator()
    .forEachRemaining(headerName -> System.out.println(headerName + ":" + request.getHeader(headerName)));
}

헤더 각각 조회

//Header 편리한 조회
private void printHeaderUtils(HttpServletRequest request) {

    System.out.println("[Host 편의 조회]");
    System.out.println("request.getServerName() = " +
    request.getServerName()); //Host 헤더
    System.out.println("request.getServerPort() = " +
    request.getServerPort()); //Host 헤더
    
    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("[cookie 편의 조회]");
    if (request.getCookies() != null) {
    for (Cookie cookie : request.getCookies()) {
    System.out.println(cookie.getName() + ": " + cookie.getValue());
    	}
    }
    
    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());
}

 


참고 강의

https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-mvc-1/dashboard

728x90
저작자표시 (새창열림)

'Spring' 카테고리의 다른 글

[스프링 MVC 1편] - HttpServletResponse  (0) 2023.01.03
[스프링 MVC 1편] - 서블릿 HttpServletRequest(http요청데이터)  (0) 2023.01.02
[스프링 MVC 1편] - 서블릿  (0) 2023.01.02
[스프링 MVC 1편] - 웹 애플리케이션 이해  (1) 2022.12.31
[spring] 게시판 CRUD 만들기  (0) 2022.12.30
  1. HttpServletRequest
  2. HttpServletRequest 역할
  3. HttpServletRequest - 기본 사용법
'Spring' 카테고리의 다른 글
  • [스프링 MVC 1편] - HttpServletResponse
  • [스프링 MVC 1편] - 서블릿 HttpServletRequest(http요청데이터)
  • [스프링 MVC 1편] - 서블릿
  • [스프링 MVC 1편] - 웹 애플리케이션 이해
study ticket
study ticket
  • study ticket
    혼자하는 공부
    study ticket
  • 전체
    오늘
    어제
    • 개발 (77)
      • 오류 (1)
      • Spring (13)
      • Java (0)
      • Data structure (6)
      • Algorithm (49)
        • 백준 (17)
        • 프로그래머스 (2)
      • 문제풀면서 알게되는것들 끄적 (2)
      • 머신러닝 (4)
        • sklearn (3)
        • pandas (1)
      • 프로젝트 (0)
        • 핏두 (0)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    백준1157
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.0
study ticket
[스프링 MVC 1편] - 서블릿 HttpServletRequest(startLine, header)
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.