image.png

1. 코드 (Get요청)

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <h1>form 태그</h1>
    <hr />
    <form action="<http://localhost:20000>" method="get">
      <input type="text" name="fullname" value="kildong" /><br />
      <input type="password" name="pw" value="1234" /><br />
      <input type="email" name="email" value="[email protected]" /><br />
      <input type="checkbox" name="ch1" value="1" />
      <input type="checkbox" name="ch2" /><br />
      <input type="date" name="birthday" /><br />
      <select name="payday">
        <option value="10">매월 10일</option>
        <option value="15">매월 15일</option>
        <option value="20">매월 20일</option>
      </select>
      <input type="submit" value="전송" />
    </form>
  </body>
</html>

2. 쿼리스트링

<http://localhost:5500/ex04/6.html>
?fullname=kildong
&pw=1234
&email=ssar%40nate.com
&ch1=1&ch2=on
&birthday=2026-01-05
&payday=10

image.png

2. 코드 (Post요청)

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <h1>form 태그</h1>
    <hr />
    <form action="<http://localhost:20000>" method="post">
      <input type="text" name="fullname" value="kildong" /><br />
      <input type="password" name="pw" value="1234" /><br />
      <input type="email" name="email" value="[email protected]" /><br />
      <input type="checkbox" name="ch1" value="1" />
      <input type="checkbox" name="ch2" /><br />
      <input type="date" name="birthday" /><br />
      <select name="payday">
        <option value="10">매월 10일</option>
        <option value="15">매월 15일</option>
        <option value="20">매월 20일</option>
      </select>
      <input type="submit" value="전송" />
    </form>
  </body>
</html>

image.png

3. 소켓 서버에 전송해서 리얼 데이터보기

package com.mtcoding;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class MyHttpServer {
    public static void main(String[] args) {
        try {
            ServerSocket ss = new ServerSocket(20000);
            Socket socket = ss.accept();

            InputStream in = socket.getInputStream();
            InputStreamReader ir = new InputStreamReader(in, StandardCharsets.UTF_8);
            BufferedReader br = new BufferedReader(ir);

            String line;
            int contentLength = 0;

            // 1) 헤더는 readLine()으로 읽는다 (빈 줄 나오면 헤더 끝)
            while ((line = br.readLine()) != null) {
                System.out.println("[server] " + line);

                if (line.toLowerCase().startsWith("content-length:")) {
                    contentLength = Integer.parseInt(line.split(":")[1].trim());
                }

                if (line.isEmpty()) break; // 헤더 끝
            }

            // 2) 바디는 read()로 contentLength 만큼 읽는다 (이게 핵심!)
            char[] body = new char[contentLength];
            int readTotal = 0;
            while (readTotal < contentLength) {
                int r = br.read(body, readTotal, contentLength - readTotal);
                if (r == -1) break;
                readTotal += r;
            }

            System.out.println("[server] ===== BODY =====");
            System.out.println(new String(body, 0, readTotal));
            System.out.println("[server] ==============");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Get 데이터

[server] GET /?fullname=kildong&pw=1234&email=ssar%40nate.com&ch1=1&ch2=on&birthday=2026-01-05&payday=10 HTTP/1.1
[server] Host: localhost:20000
[server] Connection: keep-alive
[server] sec-ch-ua: "Google Chrome";v="143", "Chromium";v="143", "Not A(Brand";v="24"
[server] sec-ch-ua-mobile: ?0
[server] sec-ch-ua-platform: "Windows"
[server] Upgrade-Insecure-Requests: 1
[server] User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36
[server] Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
[server] Sec-Fetch-Site: cross-site
[server] Sec-Fetch-Mode: navigate
[server] Sec-Fetch-User: ?1
[server] Sec-Fetch-Dest: document
[server] Referer: <http://127.0.0.1:5500/>
[server] Accept-Encoding: gzip, deflate, br, zstd
[server] Accept-Language: ko,en;q=0.9
[server] 

Post 데이터