728x90
반응형
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%-- src/main/webapp/ch03/exam2.jsp --%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><%=request.getParameter("num") %>까지의 합</title>
</head>
<body>
<%
int num = Integer.parseInt(request.getParameter("num"));
String kbn = request.getParameter("kbn");
int sum = 0;
for(int i=0; i <= num; i++) {
switch(kbn) {
case "0": sum += i; break;
case "1": if(i%2 == 0) sum += i; break;
case "2": if(i%2 == 1) sum += i; break;
}
}
%>
<%=num %> 까지의
<%=(kbn.equals("0"))? "전체합계" : kbn.equals("1") ? "짝수합계" : "홀수합계" %>
:<%=sum %>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%-- src/main/webapp/ch03/examForm1.jsp --%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>합계 계산하기</title>
<script type="text/javascript">
function inputcheck(f) {
if(f.num.value == "") {
alert("숫자를 입력하세요");
f.num.focus();
return false;
}
//isNan : 숫자가 아닌 경우
if(isNaN(f.num.value)){
alert("숫자만 입력하세요");
f.num.focus();
f.num.value = "";
return false;
}
return true;
}
function inputcheck2 (f,n) {
f.kbn.value= n;
if(f.num.value == "") {
alert("숫자를 입력하세요");
f.num.focus();
return;
}
if(isNaN(f.num.value)){
alert("숫자만 입력하세요");
f.num.focus();
f.num.value = "";
return;
}
f.submit(); // form 에 등록된 action 속성의 페이지로 요청하도록 함. submit 버튼이 클릭된 효과임.
}
</script>
</head>
<body>
<%-- 입력받은 숫자까지의 합을 구하기. 단 입력된값이 없거나 숫자가 아닌 경우 오류 발생시키기 --%>
<form action="exam1.jsp" method="post" onsubmit="return inputcheck(this)">
<input type="text" name="num">까지의 합 구하기<br>
<input type="submit" value="전체합" >
</form>
<hr>
<%-- 입력받은 숫자까지의 합(전체,짝수,홀수)을 구하기. 단 입력된값이 없거나 숫자가 아닌 경우 오류 발생시키기 --%>
<form action="exam2.jsp" method="post">
<input type="hidden" name="kbn" value="">
<input type="text" name="num">까지의 합 구하기<br>
<input type="button" value="전체합" onclick="inputcheck2(this.form,0)">
<input type="button" value="짝수합" onclick="inputcheck2(this.form,1)">
<input type="button" value="홀수합" onclick="inputcheck2(this.form,2)">
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%-- src/main/webapp/ch03/exam1.jsp --%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><%=request.getParameter("num") %>까지의 합</title>
</head>
<body>
<%
int num = Integer.parseInt(request.getParameter("num"));
int sum = 0;
for (int i=1; i<=num; i++) {
sum += i;
}
%>
<%=num %> 까지의 전체합계 : <%=sum %>
</body>
</html>
728x90
반응형
'study > Jsp' 카테고리의 다른 글
[Jsp] 9. forward 예제 (0) | 2022.04.06 |
---|---|
[Jsp] 8. response 객체 예제 : 응답객체 (0) | 2022.04.06 |
[Jsp] 7. 전송된 파라미터 값 출력 (submit, method="post/get", String request.getParamter("파라미터이름"), String[] request.getParamterValues("파라미터이름"), Enumeration request.getParameterNames()) (0) | 2022.04.06 |
[Jsp] 6. 스크립트 예제 (스크립트릿<% %>, 표현식<%= %>) (0) | 2022.04.06 |
[Jsp] 5. include 지시어 (include file) (0) | 2022.04.06 |