실습 : 계산기 구현
📄 calcForm.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Simple Calculator</title>
</head>
<body>
<h2>계산기 JSP</h2>
<hr>
<form method="post" action="calc.jsp">
<input type="text" name="n1" size="10"> <select name="op">
<option selected>+</option>
<option>-</option>
<option>*</option>
<option>/</option>
</select> <input type="text" name="n2" size="10">
<input type="submit" value="실행">
</form>
</body>
</html>
📄 calc.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
int n1 = Integer.parseInt(request.getParameter("n1"));
int n2 = Integer.parseInt(request.getParameter("n2"));
long result = 0;
switch(request.getParameter("op")) {
case "+":
result = n1 + n2;
break;
case "-":
result = n1 - n2;
break;
case "*":
result = n1 * n2;
break;
case "/":
result = n1 / n2;
break;
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>계산 결과</h2>
<hr>
결과 : <%=result%>
</body>
</html>
실습 : 계산기 구현 - 결과물
📌 실습 : 계산기 구현 - 결과물
'JSP' 카테고리의 다른 글
[JSP] 실습 : 계산기 구현(액션 버전) (0) | 2022.11.18 |
---|---|
[JSP] 액션 태그 (0) | 2022.11.18 |
[JSP] 실습 : JSP 기초 예제 (0) | 2022.11.16 |
[JSP] 템플릿 데이터와 스크립트 요소 (0) | 2022.11.16 |
[JSP] JSP 지시어 - page, include, taglib (0) | 2022.11.16 |
댓글