JSP - request.getParameter("name")와 친해지길 바라! (2022-09-21)

2022. 9. 21. 23:463층 1구역 - 개발의 장/JSP

1. 서론

 

이번에는 request.getParameter("name")과 조금 더 친해지길 바라는 의미로 퀴즈 4가지를 준비했다.

예제 2, 3번과 유사한 내용이 많기 때문에 참고하면서 풀면 쉽게 풀지 않을까??

 

 

1-1. 자기소개 적어보는 퀴즈

 

 

 

 

1-2. 학력, 소속 국가, 관심 분야 선택하는 퀴즈

 

소속국가는 칸만 늘리고 단일 선택이며,

관심 분야다중 선택이 가능하도로 해줬다.

 

 

1-3. 설문조사 하는 퀴즈

 

연령 조사는 당연하게도 단일선택

취미 조사는 다중선택이 가능하다.

 

 

1-4. BMI 측정기

 

 

 

 

2. 본론

 

2-1. 자기소개 적어보는 퀴즈

더보기

infoForm.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ex3_form</title>
<!-- http://localhost:8085/jspExam/implicit/infoForm.jsp -->
</head>
<body>
	<form action="infoResult.jsp" method="post">
		ID : <input type="text" name="name"><br>
		Password : <input type="password" name="pass"><br><br>
		자기소개<br>
		<textarea rows="3" cols="50" name="info"></textarea><br><br>
		<input type="submit" value="전송">
		<input type="reset" value="초기화">
	</form>
	
	
</body>
</html>

 

infoResult.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
    <%  request.setCharacterEncoding("UTF-8"); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>infoResult</title>
<!-- http://localhost:8085/jspExam/implicit/infoResult.jsp -->
</head>
<body>
	<h2>입력한 정보는 아래와 같습니다.</h2>
	
	ID : <%= request.getParameter("name") %><br>
	Password : <%=request.getParameter("pass") %><br>
	자기소개 : <%=request.getParameter("info") %>
	
	
</body>
</html>

 

2-2. 학력, 소속 국가, 관심 분야 선택하는 퀴즈

더보기

selectForm_answer.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>selectForm</title>
</head>
<body>
<form action="selectResult_answer.jsp" method="post">
	<table>
		<tr><td>학력</td>
			<td><select name="edu">
					<option>재학생</option>
					<option>학사</option>
					<option>석사</option>
					<option>박사</option>
				</select>
			</td>
		</tr>
		<tr>
			<td> 소속 국가 </td>
			<td><select name="country" size="4">
				<option>대한민국</option>
				<option>미국</option>
				<option>중국</option>
				<option>일본</option>
			</select>
			</td>
		</tr>
		<tr>
			<td> 관심 분야 </td>
			<td>
			<select name="like" size="4" multiple>
				<option>IT 컨설팅</option>
				<option>프로그래머</option>
				<option>시스템엔지니어</option>
				<option>네트워크엔지니어</option>
			</select>
			</td>
		</tr>
	</table>
	<input type="submit" value="전송">
	<input type="reset" value="초기화">
</form>
</body>
</html>

 

selectResult_answer.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>selectResult</title>
</head>
<body>
	<h2>입력한 정보는 아래와 같습니다.</h2>
	<%
		request.setCharacterEncoding("utf-8");
		String edu = request.getParameter("edu");
		String country = request.getParameter("country");
		String[] likes = request.getParameterValues("like");
		out.print("학력 : " + edu + "<br>");
		out.print("국가 : " + country + "<br>");
		out.print("관심 분야 : ");
		for(int i = 0; i < likes.length; i++){
			out.print(likes[i] + " ");
		}
	%>
</body>
</html>

 

2-3. 설문조사 하는 퀴즈

더보기

researchForm.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>selectForm</title>
<!-- http://localhost:8085/jspExam/implicit/selectForm.jsp -->
</head>
<body>
	<form action="researchResult.jsp" method="post">
		이름 : <input type="text" name="name"><br><br>
		
		자기소개<br>
		<textarea rows="5" cols="30" name="info"></textarea><br><br>
		
		<fieldset>
		<legend>연령조사</legend>
		10대<input type="radio" name = age value="10대">
		20대<input type="radio" name = age value="20대">
		30대<input type="radio" name = age value="30대">
		40대<input type="radio" name = age value="40대">
		</fieldset><br><br>
		
		<fieldset>
		<legend>취미조사</legend>
		독서<input type="checkbox" name="like" value="독서"> 
		춤추기<input type="checkbox" name="like" value="춤추기"> 
		멍때리기<input type="checkbox" name="like" value="멍때리기"> 
		</fieldset><br><br>
		
		<input type="submit" value="전송">
	</form>
	
	
</body>
</html>

 

researchResult.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>selectResult</title>
</head>
<body>
	<h2>입력한 정보는 아래와 같습니다.</h2>
	<%
		request.setCharacterEncoding("utf-8");
		String name = request.getParameter("name");
		String info = request.getParameter("info");
		String age = request.getParameter("age");
		String[] likes = request.getParameterValues("like");
		out.print("이름 : " + name + "<br>");
		out.print("자기소개 : " + info + "<br>");
		out.print("연령조사 : " + age + "<br>");
		out.print("취미조사 : ");
		for(int i = 0; i < likes.length; i++){
			out.print(likes[i] + " ");
		}
	%>
</body>
</html>

 

2-4. BMI 측정기

 

더보기

bmiForm.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>bmiForm</title>
<!-- http://localhost:8085/jspExam/implicit/bmiForm.jsp -->
</head>
<body>
	<form action="bmiResult.jsp" method="post">
	<%-- 사용자로부터 이름, 키, 체중 값을 입력 받아 비만도를 구하고 결과를 출력할 때
	비만도 값 100을 기준으로 100미만이면 저체중, 100이상 110미만을 정상, 110이상 120미만 과체중
	120이상 130미만 비만, 130이상은 고도비만으로 출력하세요 --%>
	
	<h1>BMI 측정기</h1>
	
	<label>이름</label><input type="text" name="name"><br>
	<label>키</label><input type="text" name="height"><br>
	<label>체중</label><input type="text" name="kg"><br>
	<input type="submit" value="전송">	
	<input type="reset" value="초기화">	
	</form>
	
	
</body>
</html>

 

bmiResult.jsp

 

<%@page import="java.text.Format"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
    <%  request.setCharacterEncoding("UTF-8"); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>bmiResult</title>
<!-- http://localhost:8085/jspExam/implicit/bmiResult.jsp -->
</head>
<body>
	<h2>입력한 정보는 아래와 같습니다.</h2>
	
	<%
		request.setCharacterEncoding("utf-8");
		String name = request.getParameter("name");
		String height = request.getParameter("height");
		String kg = request.getParameter("kg");
		out.print("이름 : " + name + "<br>");
		out.print("키 : " + height + "<br>");
		out.print("체중 : " + kg + "<br>");
		
		double height1 =  Double.parseDouble(height);
		double kg1 = Double.parseDouble(kg);
		
		/*비만도 계산식 : 비만도(%) = 현재 체중 / 표준 체중 * 100
		표준 체중 계산식 : 표준 체중 = (현재 키 - 100) * 0.9*/
		double standard = (height1 - 100) * 0.9;
		double fat = kg1 / standard * 100;
		
		String result = "";
		if(fat < 100){
			result = "저체중";
		}else if (fat >= 100 && fat < 110){
			result = "정상";
		}else if (fat >= 110 && fat < 120){
			result = "과체중";
		}else if (fat >= 120 && fat < 130){
			result = "비만";
		}else{
			result = "고도비만";
		}
		/*출력 예제 : 홍길동 님의 비만도는 112.15%로 과체중 입니다.*/
		out.print(name + "님의 비만도는 " + String.format("%.2f", fat) + "%로 " + result + "입니다.");
		
	%>
	
</body>
</html>