본문 바로가기
Backend

[스프링 MVC] HTTP 요청 파라미터 - 쿼리 파라미터, HTML Form

by 햣둘 2025. 3. 15.

HTTP 요청 데이터 조회 - 개요

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

그리소 서블릿으로 학습했던 내용을 스프링이 얼마나 깔끔하고 효율적으로 바꾸어주는지 알아보자.

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

 

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

1) GET - 쿼리 파라미터

/url**?username=hello&age=20**

메세지 바디 없이, URL의 쿼리 파라미터에 데이터를 포함해서 전달

예) 검색, 필터, 페이징 등에서 많이 사용하는 방식

 

2) POST - HTML Form

content-type:application/x-www-from-urlencoded

메세지 바디에 쿼리 파라미터 형식으로 전달 username=hello&age=20

예) 회원가입, 상품주문, HTML Form 사용

 

3) HTTP message body에 데이터를 직접 담아서 요청

HTTP API에서 주로 사용, JSON, XML, TEXT

데이터 형식은 주로 JSON 사용

POST, PUT, PATCH

 

하나씩 알아보자.

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

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

 

1) GET, 쿼리 파라미터 전송

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

 

2) POST, HTML Form 전송

예시)

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

username=hello&age=20

 

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

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

지금부터 스프링으로 요청 파라미터를 조회하는 방법을 단계적으로 알아보자.

 

RequestParamController

package hello.springmvc.basic.request;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

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");
    }
}

 

request.getParameter()

여기서는 단순히 HttpServletRequest가 제공하는 방식으로 요청 파라미터를 조회했다.

 

Get 실행

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

 

Post Form 페이지 생성

먼저 테스트용 HTML Form을 만들어야 한다.

리소스는 /resource/static 아래에 두면 스프링 부트가 자동으로 인식한다.

main/resources/static/basic/hello-form.html

<!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>

 

Post Form 실행

http://localhost:8080/basic/hello-form.html

 

참고) Jar를 사용하면 webapp 경로를 사용할 수 없다. 이제부터 정적 리소스도 클래스 경로에 함께 포함해야 한다.