본문 바로가기
JSP 웹프로그래밍

JSP : 계산기 (html, jsp파일)

by haheehee 2023. 1. 4.
<body>
	<h2>계산기 서블릿</h2>
	<form name="frmCalc" method="post" action="ch06/calc.jsp">
		<input type="text" name="num1"> 
		<select name="op">
			<option selected>+</option>
			<option>-</option>
			<option>*</option>
			<option>/</option>
		</select>
		<input type="text" name="num2"> 
		<input type="submit" value="실행">
	</form>
</body>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Calculation</title>
</head>
<%
int n1 = Integer.parseInt(request.getParameter("num1"));
int n2 = Integer.parseInt(request.getParameter("num2"));
String op = request.getParameter("op");
long result = 0;
switch(op) {
case "+": result = n1+n2; break;
case "-": result = n1-n2; break;
case "*": result = n1*n2; break;
case "/": result = n1/n2; break;
}
%>
<body>
	<h2>계산결과 - jsp</h2>
	<hr>
	결과 : <%=result %>
</body>
</html>

댓글