본문 바로가기
[Naver Cloud Camp 7] 교육 정리

네이버 클라우드 캠프 74일차 230808

by 우기37 2023. 8. 8.

### 교육정리

1) HttpServlet과 HTTP메서드

2) HTTP 프로토콜 - GET 요청과 응답

3) URL Encoding(Percent Encoding)

 

 

 

#1 HttpServlet과 HTTP메서드

 

 

HttpServlet과 HTTP 메서드에 대해서 위의 그림을 아래에서 코드와 함께 정리해보겠습니다.

 

package eomcs.servlet.ex01;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/ex01/s4")
public class Servlet04 extends HttpServlet {

  private static final long serialVersionUID = 1L;

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    System.out.println("GET 요청 받음!");

    resp.setContentType("text/plain;charset=UTF-8");
    PrintWriter out = resp.getWriter();
    out.println("GET 요청을 받았습니다~~");
  }

  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    System.out.println("POSt 요청 받음!");

    resp.setContentType("text/plain;charset=UTF-8");
    PrintWriter out = resp.getWriter();
    out.println("POST 요청을 받았습니다~~");
  }

}

 

다음과 같이 @WebServlet 애노테이션의 /ex01/s4라는 경로로 지정이 되어 있는 HttpServlet을 상속받는 클래스가 하나 있습니다.

그림처럼 HEAD와 POST, GET을 Http 통신을 통해 요청 받았을 때, 먼저 Servlet 인터페이스의 Service()에서 확인을 합니다.

 

오리지날 service에서 HEAD와 POST, GET 메서드를 호출하는지 확인하고 없다면 다음 Servlet04 코드에서 찾아봅니다.

POST와 GET의 메서드가 위의 코드에서 정의가 되어 있지만, 본 코드에서 정의하는것이 아니고 상속받는 HttpServlet에서 경로를 타고 들어가서 찾아줍니다.

 

HttpServlet의 클래스 경로를 타고 들어가면 오리지날service 메서드가 ServletRequest와 ServletResponse를 받고 있습니다. 그런데 파라미터인 req와 res는 아래에서 HttpServletRequest와 HttpServletResponse로 형변환이 되고 있습니다.

 

 

 

 

그래서 실제로 호출을 해주는 메서드는 HttpServlet의 파라미터 값을 주는 아래 service()에서 POST, GET, HEAD를 찾을 수 있습니다.

 

 

그래서 이클립스로 아웃라인을 보면 아래와 같이 HttpServlet 클래스 안에 Servlet을 받는 초록 service() 가 아닌, HttpServlet의 노란 마름모 service()를 실제로 호출하게 됩니다.

 

 

 

#2 HTTP 프로토콜 - GET 요청과 응답

 

아래 요청과 응답 내용을 확인하기 위해서 Tool 2가지를 설치하고 시작합니다.

 

 

 

 

WebDebburgingProxyApplication)

https://www.charlesproxy.com/

 

Charles Web Debugging Proxy • HTTP Monitor / HTTP Proxy / HTTPS & SSL Proxy / Reverse Proxy

Charles is an HTTP proxy / HTTP monitor / Reverse Proxy that enables a developer to view all of the HTTP and SSL / HTTPS traffic between their machine and the Internet. This includes requests, responses and the HTTP headers (which contain the cookies and c

www.charlesproxy.com

 

Request 할 Postman Tool)

https://www.postman.com/

 

Postman API Platform | Sign Up for Free

Postman is an API platform for building and using APIs. Postman simplifies each step of the API lifecycle and streamlines collaboration so you can create better APIs—faster.

www.postman.com

 

마이 워크스페이스에서 아래와 같이 servlet-app이라는 Collections를 만들고 아래와 같이 요청 할 GET, POST, HEAD, PUT 을 생성해줍니다.

 

 

후에 저장 후 Send 를 클릭하고 아래와 같이 Charles를 실행시켜 들어가면 http://192.168.0.39:8888의 ex01/s4로 응답이 된 것을 확인 할 수 있습니다.

(Contents - Raw에서 확인 가능)

 

Charles를 통해 위의 구조와 같이 요청과 응답을 모니터링 즉시 검사 할 수 있습니다.

 

 

 

그리고 아래 w3.org에서 각 프로토콜 API에 대한 정리를 볼 수 있습니다.

 

 

 

https://www.w3.org/Protocols/rfc2616/rfc2616.html

 

Hypertext Transfer Protocol -- HTTP/1.1

Network Working Group R. Fielding Request for Comments: 2616 UC Irvine Obsoletes: 2068 J. Gettys Category: Standards Track Compaq/W3C J. Mogul Compaq H. Frystyk W3C/MIT L. Masinter Xerox P. Leach Microsoft T. Berners-Lee W3C/MIT June 1999 Hypertext Transfe

www.w3.org

 

 

GET 요청과 POST 요청에서 데이터를 보내는 방법)

 

 

 

 

 

 

 

#3 URL Encoding(Percent Encoding)

 

과거에 URL은 아래와 같이 기타문자에 대해 7bit로 이루어져 있습니다.

그래서 요청하는 입장에서도 7bit로 요청을 해야하고 요청 받은사람이 다시 8bit로 변환해서 해석하고 다시 응답 할 때는 7bit로 변환해서 응답해야합니다.

 

전세계의 모든 URL을 8bit로 통일 하지 않는 이상 모든 방법을 다 알고 있어야 하기에 아래와 같이 공부했습니다.