본문 바로가기

JSP, Servlet/JSP, Servlet 기초

[JSP/Servlet] request 내장객체 실습

requset 내장객체를 이용하여 설문조사항목을 만들어 데이터를 전달하는 페이지를 작성해보자

 

<researchForm.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>
<form method="GET" action="research.jsp"> 
    <table>
        <tr>
            <td>이름</td>
            <td><input type="text" id="name" name="name"></td>
        </tr>
        <tr>
            <td>성별</td>
            <td>
                <input type="radio" id="man" name="gender" value="남자" checked="checked">남자
                <input type="radio" id="woman" name="gender" value="여자">여자
            </td>
        </tr>
        <tr>
            <td>좋아하는 계절</td>
            <td>
                <input type="checkbox" name="season" value="봄">봄
                <input type="checkbox" name="season" value="여름">여름
                <input type="checkbox" name="season" value="가을">가을
                <input type="checkbox" name="season" value="겨울">겨울
            </td>
        </tr>
        <tr>
            <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>설문조사결과</title>
</head>
<body>
	<h3>설문조사 결과</h3>
	이름 :
	<b><%=request.getParameter("name")%></b>
	<br> 성별 :
	<b><%=request.getParameter("gender")%></b>
	<br> 당신이 좋아하는 계절은
	<b><%
		String seasons[] = request.getParameterValues("season");
			for (String season : seasons) {
			out.println("[" + season + "] ");
		}
		%></b>입니다.
</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>
	<style type="text/css">
	b {
		font-size: 16pt;
	}
	</style>
	</head>
	<body>
		<h2>설문 조사 결과</h2>
		<%
		request.setCharacterEncoding("UTF-8");
		String name = request.getParameter("name");
		out.println("이름 : <b>" + name +"</b><br>");
		String gender = request.getParameter("gender");
		out.println("성별 : ");
		if (gender.equals("male")) {
			out.println("<b>남자</b><br>");
		} else {
			out.println("<b>여자</b><br>");
		}
		String seasonArr[] = request.getParameterValues("season");
		out.println("당신이 좋아하는 계절은 ");
		for (String season : seasonArr) {
			int n = Integer.parseInt(season);
			switch (n) {
			case 1: out.println("<b> 봄 </b>입니다.");
			break;
			case 2: out.println("<b> 여름 </b>입니다.");
			break;
			case 3: out.println("<b> 가을 </b>입니다.");
			break;
			case 4: out.println(" 겨울 </b>입니다.");
			break;
			}
		}
		%>
		<br>
		<b><a href='javascript:history.go(-1)'>다시</a></b>
	</body>
</html>
728x90
반응형