본문 바로가기
Spring/JSP

[JSP] 회원정보 받기

by JJH0100 2022. 11. 14.
728x90
반응형

request.jsp

입력하는 페이지

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>설문 조사표</title>
</head>
<body>
<h2>설문 조사</h2>
<form method="post" action="research.jsp">
<table border="1">
	<tr>
		<td>이 름 : </td>
		<td><input type="text" name="name" size="20"></td>		
	</tr>
	<tr>
		<td>성 별 : </td>
		<td>
			<input type="radio" name="gender" value="male" checked="checked">남자
			<input type="radio" name="gender" value="female">여자
		</td>		
	</tr>
	<tr>
		<td>종아 하는 계절은</td>
		<td>
			<input type="checkbox" name="season" value="1">봄
			<input type="checkbox" name="season" value="2">여름
			<input type="checkbox" name="season" value="3" checked="checked">가을
			<input type="checkbox" name="season" value="4">겨울
		</td>		
	</tr>
	<tr align="center">
		<td>
			<input type="submit" value="전송">
		</td>	
		<td>
			<input type="reset" value="취소">
		</td>		
	</tr>
</table>
</form>
</body>
</html>

 

research.jsp

입력 받는 페이지

<%@ 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>
</head>
<body>
<h2>설문 조사 결과</h2>
<%
	request.setCharacterEncoding("utf-8");
	String name = request.getParameter("name");
	out.println("이름 : " + name + "<br>");
	
	String gender = request.getParameter("gender");
	if(gender.equals("male")){
		out.println("성별 : 남자 <br>");
	}else if(gender.equals("female")){
		out.println("성별 : 여자 <br>");
	}
	
	/* 값이 여러개일 때는 getParameterValues */
	String season[] = request.getParameterValues("season");
	out.print("당신이 좋아하는 계절은 : ");
    
	for(String a : season){ //확장 된 for문
		/* 넘어온 값이 숫자이지만 String으로 넘어오기 때문에 int로 형변환 해주어야 함 */
		int n = Integer.parseInt(a);
		switch(n){
		case 1 :
			out.print("봄 ");
			break;
		case 2 :
			out.print("여름 ");
			break;
		case 3 :
			out.print("가을 ");
			break;
		case 4 :
			out.print("겨울 ");
			break;
		}
	}
	out.print("입니다.");
%>
</body>
</html>

 

 

Switch문 부분을 다음과 같이 쓸수도 있다.

for(String a : season){
    switch(a){
    case "1" :
        out.print("봄 ");
        break;
    case "2" :
        out.print("여름 ");
        break;
    case "3" :
        out.print("가을 ");
        break;
    case "4" :
        out.print("겨울 ");
        break;
    }
}

java최신버전 부터는 switch의 매개변수로 문자열도 사용할 수 있게 되었기 때문에 위처럼도 사용가능하다.

728x90
반응형

'Spring > JSP' 카테고리의 다른 글

[JSP] getParameterValues로 checkbox 값 넘기기  (0) 2022.11.15
[JSP] 로그인 기능  (0) 2022.11.15
[JSP] JSP의 선언부  (0) 2022.11.14
[JSP] form전송 시 get과 post의 차이  (0) 2022.11.14
[JSP] Apache 설치  (1) 2022.11.14

댓글