- 
          
          [김영한 스프링] 05. 서블릿 - HTTP 요청 데이터(GET 쿼리 파라미터, POST HTML Form)Spring/스프링 MVC 1편 - 백엔드 웹 개발 핵심 기술 2023. 6. 15. 22:44HTTP 요청 데이터 - 개요- GET - 쿼리 파라미터
- /url?username=hello&age=20
- 메시지 바디 없이, URL의 쿼리 파라미터에 데이터를 포함해서 전달
- 예) 검색, 필터, 페이징 등에서 많이 사용하는 방식
 
- POST - HTML Form
- content-type:application/x-www-form-urlencoded
- 메시지 바디에 쿼리 파라미터 형식으로 전달 username=hello&age=20
- 예) 회원 가입, 상품 주문, HTML Form 사용
 
- HTTP message body에 데이터를 직접 담아서 사용
- HTTP API에서 주로 사용, JSON, XML, TEXT
- 데이터 형식은 주로 JSON 사용
- POST, PUT, PATCH
 
 HTTP 요청 데이터 - GET 쿼리 파라미터 main/java/hello.servlet/basic/request/RequestParamServlet 생성 RequestParamServlet  실행  http://localhost:8080/request-param 결과  실행 - 파라미터 전송  http://localhost:8080/request-param?username=hello&age=20 결과  RequestParamServlet  결과  RequestParamServlet package hello.servlet.basic.request; 
 import javax.servlet.ServletException;
 import javax.servlet.annotation.WebServlet;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
 import java.util.Enumeration;
 /**
 * 1. 파라미터 전송 기능
 * http://localhost:8080/request-param?username=hello&age=20
 */
 @WebServlet(name = "RequestParamServlet", urlPatterns = "/request-param")
 public class RequestParamServlet extends HttpServlet {
 
 @Override
 protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 
 System.out.println("[전체 파라미터 조회] - start");
 
 request.getParameterNames().asIterator().forEachRemaining(parameterName -> System.out.println(parameterName + " = " + request.getParameter(parameterName)));
 
 System.out.println("[전체 파라미터 조회] - end");
 System.out.println();
 
 System.out.println("[단일 파라미터 조회] - start");
 
 String username = request.getParameter("username");
 String age = request.getParameter("age");
 
 System.out.println("username = " + username);
 System.out.println("age = " + age);
 
 System.out.println("[단일 파라미터 조회] - end");
 System.out.println();
 
 System.out.println("[이름이 같은 복수 파라미터 조회] - start");
 
 String[] usernames = request.getParameterValues("username");
 for (String name : usernames) {
 System.out.println("usernames = " + name);
 }
 
 System.out.println("[이름이 같은 복수 파라미터 조회] - end");
 
 response.getWriter().write("ok");
 }
 } 실행 - 동일 파라미터 전송  http://localhost:8080/request-param?username=hello&age=20&username=hello2 결과  복수 파라미터에서 단일 파라미터 조회 username=hello&username=kim과 같이 파라미터 이름은 하나인데, 값이 중복이면 어떻게 될까? request.getParameter()는 하나의 파라미터 이름에 대해서 단 하나의 값만 있을 때 사용해야 한다. 지금처럼 중복일 때는 request.getParameterValues()를 사용해야 한다. 참고로 이렇게 중복일 때 request.getParameter()를 사용하면 request.getParameterValues()의 첫 번째 값을 반환한다. HTTP 요청 데이터 - POST HTML Formhello-form.html <!DOCTYPE html> 
 <html>
 <head>
 <meta charset="UTF-8">
 <title>Title</title>
 </head>
 <body>
 <form action="/request-param" method="post">
 username: <input type="text" name="username" />
 age: <input type="text" name="age" />
 <button type="submit">전송</button>
 </form>
 </body>
 </html> main/webapp/basic/hello-form.html 생성 실행  http://localhost:8080/basic/hello-form.html 결과   application/x-www-form-urlencoded 형식은 앞서 GET에서 살펴본 쿼리 파라미터 형식과 같다. 따라서 쿼리 파라미터 조회 메서드를 그대로 사용하면 된다. 클라이언트(웹 브라우저) 입장에서는 두 방식에 차이가 있지만, 서버 입장에서는 둘의 형식이 동일하므로, request.getParameter()로 편리하게 구분 없이 조회할 수 있다. 정리하면 request.getParameter()는 GET URL 쿼리 파라미터 형식도 지원하고, POST HTML Form 형식도 둘 다 지원한다. 참고 
 content-type은 HTTP 메시지 바디의 데이터 형식을 지정한다.
 GET URL 쿼리 파라미터 형식으로 클라이언트에서 서버로 데이터를 전달할 때는 HTTP 메시지 바디를 사용하지 않기 때문에 content-type이 없다.
 POST HTML Form 형식으로 데이터를 전달하면 HTTP 메시지 바디에 해당 데이터를 포함해서 보내기 때문에 바디에 포함된 데이터가 어떤 형식인지 content-type을 꼭 지정해야 한다. 이렇게 폼으로 데이터를 전송하는 형식을 application/x-www-form-urlencoded라 한다.출처 : https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-mvc-1 스프링 MVC 1편 - 백엔드 웹 개발 핵심 기술 - 인프런 | 강의 웹 애플리케이션을 개발할 때 필요한 모든 웹 기술을 기초부터 이해하고, 완성할 수 있습니다. 스프링 MVC의 핵심 원리와 구조를 이해하고, 더 깊이있는 백엔드 개발자로 성장할 수 있습니다., - www.inflearn.com 'Spring > 스프링 MVC 1편 - 백엔드 웹 개발 핵심 기술' 카테고리의 다른 글[김영한 스프링] 07. 서블릿 - HttpServletResponse (0) 2023.06.16 [김영한 스프링] 06. 서블릿 - HTTP 요청 데이터(API 메시지 바디) (0) 2023.06.16 [김영한 스프링] 04. 서블릿 - HttpServletRequest (0) 2023.06.14 [김영한 스프링] 03. Hello 서블릿 (0) 2023.06.12 [김영한 스프링] 02. 서블릿 - 프로젝트 생성 & 세팅 (0) 2023.06.11 
- GET - 쿼리 파라미터