Notice
Recent Posts
Recent Comments
Link
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

개발자되기 프로젝트

HttpServletRequest 사용법 본문

인프런/[인프런] 스프링 MVC 1

HttpServletRequest 사용법

Seung__ 2021. 9. 7. 23:33

 1. RequestHeaderServlet CLass


@WebServlet(name = "requestHeaderServlet", urlPatterns = "/request-header")
public class RequestHeaderServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        printStartLine(request);
    }

    private void printStartLine(HttpServletRequest request) {
        //start line정보 가져오기
        System.out.println("--- REQUEST-LINE - start ---");
        System.out.println("request.getMethod() = " + request.getMethod());
        //GET
        System.out.println("request.getProtocal() = " + 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-test
        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 ---");
        System.out.println();
    }
}
  • http://localhost:8080/request-header 로 들어가보자.
  • startline 정보를 가지고올 수 있당 ㅋㅋ
--- REQUEST-LINE - start ---
request.getMethod() = GET
request.getProtocal() = HTTP/1.1
request.getScheme() = http
request.getRequestURL() = http://localhost:8080/request-header
request.getRequestURI() = /request-header
request.getQueryString() = null
request.isSecure() = false
--- REQUEST-LINE - end ---

 

 

2. Header정보 모두 가져오기


 

2.1 예전 방법

  • request에서 getHeaderNames를 가져옴
  • HeaderNames에 다음 값이 있을 경우 값을 꺼내서 출력해봄.
   @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        printStartLine(request);
        printHeaders(request);
    }


    //Header 모든 정보
    public void printHeaders(HttpServletRequest request){
        System.out.println("--- Headers-LINE - start ---");

        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()){
            String headerName = headerNames.nextElement();
            System.out.println(headerName + " : headerName = " + headerName);
        }

        System.out.println("--- Headers-LINE - end ---");
        System.out.println();
    }
  • 겁나많넹 ㄷㄷ
--- Headers-LINE - start ---
host : headerName = host
connection : headerName = connection
cache-control : headerName = cache-control
sec-ch-ua : headerName = sec-ch-ua
sec-ch-ua-mobile : headerName = sec-ch-ua-mobile
upgrade-insecure-requests : headerName = upgrade-insecure-requests
user-agent : headerName = user-agent
accept : headerName = accept
sec-fetch-site : headerName = sec-fetch-site
sec-fetch-mode : headerName = sec-fetch-mode
sec-fetch-user : headerName = sec-fetch-user
sec-fetch-dest : headerName = sec-fetch-dest
accept-encoding : headerName = accept-encoding
accept-language : headerName = accept-language
cookie : headerName = cookie
--- Headers-LINE - end ---

 

 

2.2 최근 방법

  • while로 안돌리고 iterator로 변경
    //Header 모든 정보
    public void printHeaders(HttpServletRequest request){
        System.out.println("--- Headers-LINE - start ---");

        /*
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()){
            String headerName = headerNames.nextElement();
            System.out.println(headerName + " : headerName = " + headerName);
        }*/

        request.getHeaderNames()
                .asIterator()
                .forEachRemaining(headerName -> System.out.println(headerName + " : headerName = " + headerName));

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

 

 

 

 

3. Header조회방법


  • request.getLocale()을 하면 우선순위 가장 높은 언어 가져옴.
    //Header 편리한 조회
    private void printHeaderUtils(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();
    }
  • 왜 contentType이 null 이지? -> GET방식이니까 ㅋㅋ 내가 날리는게없음.
--- Header 편의 조회 start ---
[Host 편의 조회]
request.getServerName() = localhost
request.getServerPort() = 8080

[Accept-Language 편의 조회]
locale = ko_KR
locale = ko
locale = en_US
locale = en
request.getLocale() = ko_KR

[cookie 편의 조회]
Idea-9c9b0a8: e54d9da6-7c28-4f4c-b956-18e4506e5a79

[Content 편의 조회]
request.getContentType() = null
request.getContentLength() = -1
request.getCharacterEncoding() = UTF-8
--- Header 편의 조회 end ---

 

  • Header의 특정 항목만 가져오고 싶은 경우?
request.getHeader("host")

 

 

4. 기타정보


    //기타 정보
    private void printEtc(HttpServletRequest request) {
        System.out.println("--- 기타 조회 start ---");
        System.out.println("[Remote 정보]");
        System.out.println("request.getRemoteHost() = " +
                request.getRemoteHost()); //
        System.out.println("request.getRemoteAddr() = " +
                request.getRemoteAddr()); //
        System.out.println("request.getRemotePort() = " +
                request.getRemotePort()); //
        System.out.println();
        System.out.println("[Local 정보]");
        System.out.println("request.getLocalName() = " +
                request.getLocalName()); //
        System.out.println("request.getLocalAddr() = " +
                request.getLocalAddr()); //
        System.out.println("request.getLocalPort() = " +
                request.getLocalPort()); //
        System.out.println("--- 기타 조회 end ---");
        System.out.println();
    }
  • remote : 요청이 온 곳
  • local : 내 서버 정보
  • 네트워크 커넥션 정보, http messgae정보 아님.
--- 기타 조회 start ---
[Remote 정보]
request.getRemoteHost() = 0:0:0:0:0:0:0:1
request.getRemoteAddr() = 0:0:0:0:0:0:0:1
request.getRemotePort() = 54319

[Local 정보]
request.getLocalName() = 0:0:0:0:0:0:0:1
request.getLocalAddr() = 0:0:0:0:0:0:0:1
request.getLocalPort() = 8080
--- 기타 조회 end ---

 

 

5. GitHub : 210907 HttpServletRequest사용법


 

GitHub - bsh6463/MVC1

Contribute to bsh6463/MVC1 development by creating an account on GitHub.

github.com

 

'인프런 > [인프런] 스프링 MVC 1' 카테고리의 다른 글

HTTP 요청 데이터 - GET 쿼리 파라미터  (0) 2021.09.08
HTTP 요청 데이터  (0) 2021.09.08
Http Servlet Request  (0) 2021.09.07
[서블릿] Hello Servlet  (0) 2021.09.07
[서블릿] 프로젝트 생성  (0) 2021.09.07
Comments