2013년 7월 10일 수요일

(130710) 13일차 FormData.jsp, RequestTest.jsp (form method에서 post와 get의 차이, request 객체 테스트)

 - post과 get의 차이 : <form action = "....jsp" method = "post"/"get">













 - request 객체 : 클라이언트가 작성한 요청 정보를 처리하기 위해서 서버가 저장한다.


 - FormData.jsp 소스
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title> 회원 가입 </title>
</head>
<body>
<h2> 회원 가입 </h2>
<!-- post는 url에서 숨겨서 보내고, get은 url에 표기해서 보냄 -->
<form action = "RequestTest.jsp" method = "post"> 
  
  아이디 : <input type = "text" name = "id" size = "50"> <br><br>
  패스워드 : <input type = "password" name = "pass"> <br><br>
  당신의 성별은 : <input type = "radio" name = "gender" value = "남성" > 남성
      <input type = "radio" name = "gender" value = "여성"> 여성  <br><br>
  당신의 취미는 : <input type = "checkbox" name = "hobby" value = "음악감상" checked = "checked"> 음악감상
      <input type = "checkbox" name = "hobby" value = "영화감상"> 영화감상
      <input type = "checkbox" name = "hobby" value = "악기연주"> 악기연주  <br><br>
  당신의 직업은 : <select size="5" name="job">
      <option value = "학생"> 학생 </option>
      <option value = "개발자"> 개발자 </option>
<option value = "백수"> 백수 </option>
      <option value = "의사"> 의사 </option>
      <option value = "검사"> 검사 </option>
  </select> <br><br>
  하고 싶은말 : <textarea name="talk" rows="5" cols="50"></textarea> <br><br>
<input type="submit" value="전송"> <input type="reset" value="취소">

</form>
</body>
</html>


 - 결과






















 - RequestTest.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title> 회원 정보 </title>
</head>
<body>
<%
// 한글처리
request.setCharacterEncoding("euc-kr");

// 아이디
String id = request.getParameter("id");
// 패스워드
String pass = request.getParameter("pass");
// 성별
String gender = request.getParameter("gender");
// 취미 (여러개의 값을 받기위해서 배열에 넣음)
String[] hobby = request.getParameterValues("hobby");
String temp = "";
for(int i = 0 ; i < hobby.length ; i++)
temp = temp + " " + new String(hobby[i].getBytes());

// 직업
String job = request.getParameter("job");
// 하고 싶은말
String talk = request.getParameter("talk");

out.println("아이디 : " + id + "<br>");
out.println("패스워드 : " + pass+ "<br>");
out.println("성별 : " + gender+ "<br>");
out.println("취미 : " + temp+ "<br>");
out.println("직업 : " + job+ "<br>");
out.println("하고 싶은말 : " + talk+ "<br>");

%>
</body>
</html>


 - 결과













 - 취미의 여러개 value 값을 Enumeration을 이용해서 가져오는 방법
// 취미
Enumeration<String> e = request.getParameterNames();
while(e.hasMoreElements()){
String data = e.nextElement();
if(data.equals("hobby")){
String[] hobby = request.getParameterValues(data);
out.print(Arrays.toString(hobby)+"<br>");
}
else
out.print(request.getParameter(data)+"<br>");
}