개발자 미니민의 개발스터디

[JSP] 📓 파라미터 전송 예제 (폼 예제)

by mini_min

JSP

[JSP] 📓 파라미터 전송 예제 (폼 예제)

 

- 클라이언트 페이지

<%@ page contentType="text/html; charset=UTF-8"%>
<%@page trimDirectiveWhitespaces="true" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<h3> 폼 예제 </h3>

<form action="ex21_ok.jsp" method="post">
	<p> 이름 : <input type="text" name="userName"></p>
	<p> 학번 : <input type="text" name="stdId"></p>
	<p> 성별 :
		남자 <input type="radio" name="gender" value="M">
		여자 <input type="radio" name="gender" value="F" checked="checked">
	</p>
	<p>	좋아하는 과목 :
		<input type="text" name="subject">
		<input type="text" name="subject">
		<input type="text" name="subject">
	</p>
	<p> 출신도 :
		<select name="city">
			<option value="">::선택</option>
			<option value="서울">서울시</option>
			<option value="인천">인천시</option>
			<option value="경기">경기도</option>
			<option value="기타">기타</option>
		</select>
	</p>
	<p>
		취미 :
		<select name="hobby" multiple="multiple" size="5">
			<option value="운동">운동하기</option>
			<option value="영화">영화보기</option>
			<option value="등산">등산하기</option>
			<option value="여행">여행가기</option>
			<option value="게임">게임하기</option>
			
		</select>
	</p>
	<p>
		<button type="submit">보내기</button>
	</p>
</form>

</body>
</html>

 

 

- 서버 페이지

👩‍💻 동일한 이름을 가진 파라미터들은 getParameterValues 로 받아서 배열에 저장했다. (subject , hobby)

<%@ page contentType="text/html; charset=UTF-8"%>
<%@page trimDirectiveWhitespaces="true" %>
<%
	request.setCharacterEncoding("utf-8");
	
	String userName = request.getParameter("userName");
	String stdId = request.getParameter("stdId");
	String gender = request.getParameter("gender");
	
	String[] ss = request.getParameterValues("subject");
	String city = request.getParameterValues("city")[0];
	String[] hh = request.getParameterValues("hobby");
	
	String subject = "";
	if(ss != null){
		for(String s : ss){
			subject += s + " ";
		}	
	}
	
	String hobby = "";
	if(hh != null){
		for(String h : hh){
			hobby += h + " ";
		}	
	}

%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<h3> 결과 </h3>
<p> 이름 : <%=userName %> </p>
<p> 학번 : <%=stdId %> </p>
<p> 성별 : <%=gender %> </p>
<p> 좋아하는 과목 : <%=subject %> </p>
<p> 출신도 : <%=city %> </p>
<p> 취미 : <%=hobby %> </p>



</body>
</html>

 

 

 

블로그의 정보

개발자 미니민의 개발로그

mini_min

활동하기