선택장애

JSP - request사용하기 (select와 checkbox값받기) 본문

JSP

JSP - request사용하기 (select와 checkbox값받기)

yes or yes 2017. 8. 14. 14:51
반응형

request_test.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>Insert title here</title>
</head>
<body>
<form name="form1" method="post" action="request_test2.jsp">
<center>
<h1>request 테스트 폼</h1>
<hr>
<table border="1">
<tr>
<td>이름
</td>
<td><input type="text" name="username">
</td>
</tr>
<tr>
<td>직업
</td>
<td>
<select name="job">
       <option value="학생">학생</option>
       <option value="학생2">학생2</option>
       <option value="학생3">학생3</option>
       <option value="학생4">학생4</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="정보통신">정보통신
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="확인">
<input type="reset" value="취소"> 
</td> 
</tr>
</table>
</center>

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

값을 받을 request_test2.jsp

//아이디값이 아니고 name값을 주어야한다

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<% request.setCharacterEncoding("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>
<h1>결과</h1>
<hr>
<table border="1"></table>
<tr>
<td>이름</td>
<td><%=request.getParameter("username") %></td>
//이름의 name값을 받는다.
<tr>
<td>직업</td>
<td><%=request.getParameter("job") %></td>
//직업의 name값을 받는다.
<tr>
<td>관심분야
<td>
<% String[] hobby = request.getParameterValues("hobby"); 
   if (hobby != null) { 
      for (int i = 0; i < hobby.length; i++) 
         { out.println(hobby[i]); } // for 
         } // if %> 
//체크박스
</td>
</body>
</html>


반응형