2011. 9. 6. 12:26

JSP에서 Request 객체로 HTML의 변수값 받아오기

<%@ 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>Insert title here</title>
<script language="JavaScript">
function check() {
 if(form.num.value == "") {
  alert("학번을 입력하세요") ;
  form.num.focus() ;
  return ;
 }

 else if(form.name.value == "") {
  alert("이름을 입력하세요") ;
  form.name.focus() ;
  return ;
 } 
form.submit() ;
}
</script>
</head>
<body>
<h2>학번, 이름, 학년, 선택과목 입력</h2>
<form name="form" action="Request.jsp" method="post">
학번 : <input type="text" name="num"><br>
이름 : <input type="text" name="name"><p>
학년 : <input type="radio" name="grade" value="1학년">1학년
      <input type="radio" name="grade" value="2학년">2학년
      <input type="radio" name="grade" value="3학년">3학년
      <input type="radio" name="grade" value="4학년">4학년<p>
동물 : <input type="checkbox" name="animal" value="토끼">퇴끼
      <input type="checkbox" name="animal" value="강아지">강아지
      <input type="checkbox" name="animal" value="고양이">고양이
      <input type="checkbox" name="animal" value="돼지">돼지<p>
선택과목 :
<select name="subject">
 <option>JAVA</option>
 <option>JSP</option>
 <option>XML</option>
</select>   
<p>
<!-- <input type="submit" value="입력완료"> -->
<input type="button" value="입력완료" onClick="check()">
</form>   
</body>
</html>
<%@ 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>Insert title here</title>
</head>
<body>
<%
 request.setCharacterEncoding("euc-kr");
 String num = request.getParameter("num") ;
 String name = request.getParameter("name") ;
 String[] grade = request.getParameterValues("grade") ;
 String[] animal = request.getParameterValues("animal") ; 
 String subject = request.getParameter("subject") ;
%>
학번 : <% out.println(num) ; %><br>
이름 : <%=name%><br>
학년 : <%
   for(int i = 0 ; i < grade.length ; i++) {
    out.print(grade[i] + " ") ;    
   }
  %><br>
동물 : <%
   for(int i = 0 ; i < animal.length ; i++) {
    out.print(animal[i] + " ") ;    
   }
   out.println("선택") ;
  %><br>
과목 : <%=subject%><br>             
</body>
</html>