- GET방식
- url에 데이터를 포함하여 요청
- 길이 제한으로 전송 데이터 한계
- url에 데이터가 노출되어 보안 취약
www.ex.com?name=choi&id=chch
구분자 ? 뒤(쿼리스트링) 키 값 쌍으로 요청 파라미터를 전달한다.
만약, 요청 파라미터가 여러개라면 구분자 &로 구분한다.
- POST방식
- 전송할 수 있는 데이터의 길이 제한이 없다.
- 서버 쪽 작업에서 사용(데이터 기록, 삭제, 수정 ...)
GET 방식과는 달리 URL에 데이터가 노출이 안되고 문서 요청 바디에 전달이 된다. 개발자 도구의 네트워크에서 확인 가능하다.
- 폼에서 데이터 가져오기 HttpServletRequest
- HttpServletRequest에는 사용자의 요청에 대한 정보가 담긴다.
- request 내장 객체를 통해 데이터가 저장되고, getParameter() 메서드를 통해 값을 return 받는다.
request.getParameter(name);
String name = request.getParameter("name"); // choi
String id = request.getParameter("id"); // chch
request.getParameterValues(name); // 같은 이름의 여러값을 가져올 수 있다.
다음과 같이 form에서 "hobby"라는 name을 가져와 String배열에 담는다.
String[] hobby = request.getParameterValues("hobby");
for(String h : hobby) out.print(h); // for문을 이용한 출력
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
table,
td{
border : 1px solid black;
}
</style>
</head>
<body>
<form action="./signForm.jsp" method="post">
<h1>회원가입</h1>
<table>
<tr>
<td>아이디</td>
<td><input type="text" name="id"/></td>
</tr>
<tr>
<td>
<div>비밀번호</div>
<div>비밀번호 확인</div>
</td>
<td>
<div><input type="password" name="pw"/></div>
<div><input type="password" name="pwCheck"/></div>
</td>
</tr>
<tr>
<td>이름</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>생일</td>
<td><input type="date" name="birth" /></td>
</tr>
<tr>
<td>나이</td>
<td><input type="number" name="age" /></td>
</tr>
<tr>
<td>성별</td>
<td>
<input type="radio" name="gender" value="남"/>남
<input type="radio" name="gender" value="여"/>여
</td>
</tr>
<tr>
<td>메일</td>
<td>
<input type="text" name="email" />
@
<select name="domain">
<option>선택</option>
<option>naver.com</option>
<option>daum.net</option>
<option>gmail.com</option>
</select>
</td>
</tr>
<tr>
<td>취미</td>
<td>
<input type="checkbox" name="hobby" value="헬스"/>헬스
<input type="checkbox" name="hobby" value="축구"/>축구
<input type="checkbox" name="hobby" value="배드민턴"/>배드민턴
<input type="checkbox" name="hobby" value="당구"/>당구
</td>
</tr>
<tr>
<td>메모</td>
<td><textarea cols="30" rows="5" name="memo"></textarea></td>
</tr>
</table>
<input type="submit" value="회원가입"/>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
String id = request.getParameter("id");
String pw = request.getParameter("pw");
String pwCheck = request.getParameter("pwCheck");
String name = request.getParameter("name");
String birth = request.getParameter("birth");
String age = request.getParameter("age");
String gender = request.getParameter("gender");
String email = request.getParameter("email");
String domain = request.getParameter("domain");
String memo = request.getParameter("memo");
String[] hobby = request.getParameterValues("hobby");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
table,
td{
border : 1px solid black;
}
</style>
</head>
<body>
<h1>회원가입</h1>
<table>
<tr>
<td>아이디</td>
<td><%= id %></td>
</tr>
<tr>
<td>
<div>비밀번호</div>
<div>비밀번호 확인</div>
</td>
<td>
<div><%= pw %></div>
<div><%= pwCheck %></div>
</td>
</tr>
<tr>
<td>이름</td>
<td><%= name %></td>
</tr>
<tr>
<td>생일</td>
<td><%= birth %></td>
</tr>
<tr>
<td>나이</td>
<td><%= age %></td>
</tr>
<tr>
<td>성별</td>
<td>
<%= gender %>
</td>
</tr>
<tr>
<td>메일</td>
<td>
<%= email %>@<%= domain %>
</td>
</tr>
<tr>
<td>취미</td>
<td>
<ul>
<%
if(!(hobby == null))
for(String h : hobby) out.print("<li>" + h + "</li>");
%>
</ul>
</td>
</tr>
<tr>
<td>메모</td>
<td><%= memo %></td>
</tr>
</table>
</body>
</html>