2-10. HTML의 기본 태그_form 태크

박은서's avatar
Jan 05, 2026
2-10. HTML의 기본 태그_form 태크

1. input 태그

1️⃣ input 태그란?

1) 개념

사용자가 실제로 값을 입력하는 칸
type 속성에 따라 동작이 달라짐

2) 기본 형태

<input type="text" name="username">

2️⃣ input 태그의 주요 type 종류

1) 텍스트 입력

<input type="text" placeholder="이름 입력">
type
설명
text
일반 문자
password
비밀번호 (●●●)
email
이메일 형식 검사
number
숫자만 입력
tel
전화번호
url
URL 입력

2) 선택 관련

<input type="checkbox"> 동의 <input type="radio" name="gender"><input type="radio" name="gender">
type
설명
checkbox
복수 선택
radio
단일 선택 (name 같아야 함)

3) 버튼

<input type="submit" value="전송"> <input type="reset" value="초기화"> <input type="button" value="버튼">
type
설명
submit
form 전송
reset
입력값 초기화
button
JS용 버튼

4) 기타

<input type="file"> <input type="date"> <input type="color">

3️⃣ input 태그의 핵심 속성

속성
설명
name
서버로 보내질 데이터의 key
value
기본값
placeholder
안내 문구
required
필수 입력
readonly
읽기 전용
disabled
비활성화
maxlength
최대 글자 수
<input type="text" name="id" placeholder="아이디" required maxlength="12">

4️⃣ 실습

1) input:text

<input type="text" name="fullname" value="홍길동" /><br />
notion image
홍길동에 다른 이름 적으면 value에 값이 들어감

2) input:password

<input type="password" name="pw" value="1234" /><br />
notion image

3) input:email

<input type="email" name="email" value="ssar@nate.com" /><br />
notion image

4) input:checkbox

<input type="checkbox" name="ch1" value="1" /> <input type="checkbox" name="ch2" />
notion image

5) input:date

<input type="date" name="birthday" />
notion image

6) select

<select name="payday"> <option value="10">매월 10일</option> <option value="15">매월 15일</option> <option value="20">매월 20일</option> </select>
notion image
notion image

7) input:button

<input type="submit" value="전송" />
notion image
value 값이 사용자에게 보여주는 이름
타입을 submit 으로 바꾸면 그 위의 내용들을 한 번에 전송 해줌
⚠️ 타입이 button이면 전송 안 됨!

8) input 태그 전체 코드

<form> <input type="text" name="fullname" value="홍길동" /><br /> <input type="password" name="pw" value="1234" /><br /> <input type="email" name="email" value="ssar@nate.com" /><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>
 

2. form 태그

1️⃣ form 태그란?

1) 개념

사용자가 입력한 데이터를 서버로 보내기 위한 영역
로그인, 회원가입, 검색창, 설문조사 같은 기능은 전부 form으로 만들어짐

2) 기본 구조

<form action="/submit" method="post"> <!-- 입력 요소들 --> </form>

2️⃣ form 태그의 주요 속성

속성
설명
action
데이터를 보낼 서버 주소(URL)
method
전송 방식 (get 또는 post)
name
폼의 이름
target
결과를 어디에 표시할지 (_self, _blank)

3️⃣ method 차이

1) GET

  • URL에 데이터가 노출됨
  • 검색, 조회에 적합 (select 요청)
  • 예: /search?q=apple

2) POST

  • 데이터가 숨겨져 전송됨
  • 로그인, 회원가입에 적합 (update 요청(insert, delete, update))
  • 보안 & 대용량 데이터 가능

4️⃣ 실습

1) method = “get”

<form action="http://localhost:5500/ex04/6.html" method="get"> <input type="text" name="fullname" value="홍길동" /><br /> <input type="password" name="pw" value="1234" /><br /> <input type="email" name="email" value="ssar@nate.com" /><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>
notion image
notion image
주소 http://localhost:5500/ex04/6.html ?fullname=%ED%99%8D%EA%B8%B8%EB%8F%99 &pw=1234 &email=ssar%40nate.com &ch1=1&ch2=on &birthday=2026-01-05 &payday=10
➡️ key/value값이 넘어가는데, check에 value값을 넣고 체크했을 때는 1, 안 넣고 체크했을 때는 on이 날라감 → value값 넣어야 함!

💡 쿼리 스트링(Query String)

① 쿼리스트링이란?
URL 뒤에 ?를 붙여 서버로 전달하는 데이터
데이터베이스에 where 조건에 걸고 싶은 것들을 전송할 때 사용
쿼리(Query) - 질문 + 스트링(String) - 문자열 → 문자열로 질문한다
기본 형태
URL?key=value&key=value
예시
/search?keyword=apple&page=2
➡️ 서버는 이렇게 해석
  • keyword = apple
  • page = 2
② GET 방식과 쿼리 스트링의 관계
method="get"으로 form을 전송하면
📌 input 값들이 자동으로 쿼리 스트링으로 변환돼서 URL에 붙음
HTML 예제
<form action="/search" method="get"> <input type="text" name="q"> <input type="number" name="page"> <input type="submit"> </form>
사용자가 입력
  • q → banana
  • page → 3
실제 요청 URL
/search?q=banana&page=3
③ 쿼리 스트링 구성 요소
기호
의미
?
쿼리 시작
&
파라미터 구분
=
key와 value 연결
?name=kim&age=20&city=seoul
④ name 속성이 중요한 이유 ⭐
👉 input의 name 값이 key가 된다
<input type="text" name="username">
⬇️
?username=kim
❌ name이 없으면 전송 안 됨
<input type="text">
⑤ URL 인코딩 (매우 중요!)
쿼리 스트링은 URL 규칙을 따라야 해서, 한글, 공백, 특수문자는 인코딩되어 변환됨.
예시
입력값
변환
hello world
hello%20world
김철수
%EA%B9%80%EC%B2%A0%EC%88%98
&
%26
실제 URL
/search?keyword=%EA%B9%80%EC%B2%A0%EC%88%98
➡️ 브라우저가 자동으로 인코딩해줘서 개발자가 직접 할 일은 거의 없음
⑥ 같은 name을 여러 개 보낼 때
checkbox 예제
<input type="checkbox" name="food" value="pizza"> <input type="checkbox" name="food" value="chicken">
결과
?food=pizza&food=chicken
➡️ 서버에서는 배열로 받는 경우가 많음
⑦ GET 쿼리 스트링의 특징
✅ 장점
  • URL만으로 요청 재현 가능
  • 북마크, 공유 가능
  • 검색, 필터링에 적합
❌ 단점
  • URL에 데이터 노출 (보안 취약)
  • 길이 제한 있음
  • 민감한 정보에 부적합
⑧ POST 방식과 비교
항목
GET
POST
데이터 위치
URL
Body
노출
O
X
길이 제한
있음
거의 없음
북마크
가능
불가

2) method = “post”

<form action="http://localhost:5500/ex04/6.html" method="post"> <input type="text" name="fullname" value="홍길동" /><br /> <input type="password" name="pw" value="1234" /><br /> <input type="email" name="email" value="ssar@nate.com" /><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>
notion image
➡️ post한 정보는 숨겨져서 전송됨

💡 숨겨진 정보 보는 법

위의 화면에서 F12 클릭 → Network 클릭 → ctrl + R 클릭 → 6.html 클릭 → Payload 확인
notion image
 

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

1️⃣ JAVA 코드

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

2️⃣ 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]

3️⃣ Post 데이터

1) Content-Type: application/x-www-form-urlencoded

[server] POST / HTTP/1.1 [server] Host: localhost:20000 [server] Connection: keep-alive [server] Content-Length: 89 [server] Cache-Control: max-age=0 [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] Origin: http://127.0.0.1:5500 [server] Content-Type: application/x-www-form-urlencoded [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] fullname=kildong&pw=1234&email=ssar%40nate.com&ch1=1&ch2=on&birthday=2026-01-05&payday=10
notion image

⚠️ 확인해야 할 내용

  • POST 인지? GET 인지?
  • Content-Length
  • Content-Type (MIME타입)
  • BODY 내용 (Content-Length와 Content-Type을 알아야 파싱 가능)

2) JSON

notion image

3) TEXT

notion image

4) HTML

notion image

5) IMAGE

notion image
 

4. MIME 타입

1️⃣ MIME 타입이란?

MIME (Multipurpose Internet Mail Extensions)
➡️ 전송되는 데이터의 형식(type)을 명확히 알려주는 문자열
“이건 HTML이야”, “이건 JSON이야”, “이건 이미지야”
를 컴퓨터끼리 약속한 표기법이야.
웹에서 “이 데이터가 무엇인지”를 알려주는 공식적인 표기법
브라우저, 서버, 이메일, API 전부에서 핵심적으로 쓰임

2️⃣ MIME 타입의 기본 구조

1) 기본 구조

type/subtype

2) 예시

MIME 타입
의미
text/html
HTML 문서
text/css
CSS 파일
application/json
JSON 데이터
image/png
PNG 이미지
image/jpeg
JPG 이미지

3️⃣ MIME 타입은 어디서 쓰일까?

1) HTTP 응답 헤더

Content-Type: text/html
➡️ 브라우저:
“아, 이건 HTML이니까 렌더링해야겠다”

2) form 전송

<form method="post" enctype="multipart/form-data">
➡️ 서버:
“파일 포함된 데이터구나”

3) API 통신

Content-Type: application/json
➡️ 서버/클라이언트:
“JSON으로 파싱하면 되는구나”

4️⃣ 자주 쓰는 MIME 타입 정리 ⭐

1) 텍스트 계열

MIME 타입
설명
text/plain
일반 텍스트
text/html
HTML
text/css
CSS
text/javascript
JavaScript

2) 애플리케이션 계열

MIME 타입
설명
application/json
JSON 데이터
application/xml
XML
application/pdf
PDF
application/zip
ZIP 파일
application/octet-stream
알 수 없는 바이너리
➡️ octet-stream
= “이게 뭔지는 모르겠고, 그냥 파일이야”

3) 이미지 계열

MIME 타입
설명
image/png
PNG
image/jpeg
JPG
image/gif
GIF
image/svg+xml
SVG

4) 미디어 계열

MIME 타입
설명
audio/mpeg
MP3
video/mp4
MP4

5️⃣ Content-Type과의 관계 (매우 중요)

1) 서버 → 클라이언트

Content-Type: application/json
➡️ 클라이언트는 JSON으로 파싱

2) 클라이언트 → 서버

Content-Type: application/x-www-form-urlencoded
➡️ form 기본 전송 방식
Content-Type: multipart/form-data
➡️ 파일 업로드

6️⃣ form에서 쓰이는 MIME 타입

1) 기본값

<form method="post">
application/x-www-form-urlencoded
id=kim&pw=1234

2) 파일 포함

<form method="post" enctype="multipart/form-data">
➡️ 텍스트 + 파일 혼합 전송 가능

7️⃣ MIME 타입이 중요한 이유

1) ❌ 잘못된 MIME 타입

Content-Type: text/plain
(JSON 보내면서 이렇게 하면)
➡️ 브라우저/서버가 제대로 해석 못함

2) ✅ 올바른 MIME 타입

Content-Type: application/json
➡️ 자동 파싱 & 보안 처리 가능

8️⃣ 확장자 ≠ MIME 타입 (중요!)

파일
확장자
MIME 타입
index.html
.html
text/html
data.json
.json
application/json
image.jpg
.jpg
image/jpeg
➡️ 확장자는 사람 기준
➡️ MIME 타입은 컴퓨터 기준
 
Share article