서블릿에서 공부했던 HTTP 요청 데이터를 조회하는 방법을 다시 떠올려 보았다.

그리고 서블릿으로 공부했던 내용을 스프링이 얼마나 편하게 바꿔주는지 알아보았다.

 

HTTP 요청 메시지를 통해 클라이언트에서 서버로 데이터를 전달하는 방법을 알아보았다.

 

클라이언트에서 서버로 요청 데이터를 전달할 때는 주로 3가지 방법을 사용한다.

 

GET - 쿼리 파라미터

  /url?uesrname=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

 

요청 파라미터 - 쿼리 파라미터, HTML Form

HttpServletRequest의 request.getParameter()를 사용하면 두가지 요청 파라미터를 조회할 수 있다.

 

GET, 쿼리 파라미터 전송

예 ) http://localhost:8080/request-param?username=hello&age=20

 

POST, HTML Form 전송

예 )

POST /request-param ...
content-type: application/x-www-form-urlencoded

username=hello&age=20

GET 쿼리 파라미터 전송 방식이든, POST HTML Form 전송 방식이든 둘다 형식이 같으므로 구분없이

조회할 수 있다.

이것을 간단히 요청 파라미터(request parameter)조회라 한다.

package hello.springmvc.basic.request;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@Controller
public class RequestParamController {

    @RequestMapping("/request-param-v1")
    public void requestParamV1(HttpServletRequest request, HttpServletResponse response)throws IOException {
        String username = request.getParameter("username");
        int age = Integer.parseInt(request.getParameter("age"));

        log.info("username={}, age={}", username, age);
        response.getWriter().write("ok");
    }
}

단순히 서블릿과 같은 방식으로 조회했다.

<!DOCTYPE html>
<html>
<head>
 <meta charset="UTF-8">
 <title>Title</title>
</head>
<body>
 <form action="/request-param-v1" method="post">
 username: <input type="text" name="username" />
 age: <input type="text" name="age" />
 <button type="submit">전송</button>
 </form>
</body>
</html>

 HTML을 작성해서 POST방식으로도 테스트 해보았다.

이전 과는다르게 Jar를 사용하여 webapp경로를 사용할 수 없다. 이제부터 정적 리소스도 클래스 경로에 함께

포함해야 한다.

+ Recent posts