JSP

[JSP] 실습 : 계산기 구현

기록하는_사람 2022. 11. 16. 12:47

실습 : 계산기 구현

📄 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>

 

실습 : 계산기 구현 - 결과물

📌 실습 : 계산기 구현 - 결과물