<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/myjsp/calculatorController" method="post">
피연산자1 : <input type="text" name="op1"><br>
피연산자2 : <input type="text" name="op2"><br>
연산자 :
<select name="operator">
<option>+</option>
<option>-</option>
<option>/</option>
<option>X</option>
</select><br>
<input type="submit" value="계산">
</form>
</body>
</html>
package calc.controller;
import java.io.IOException;
import java.math.BigDecimal;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import calc.model.CalculatorService;
public class CalculatorServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public CalculatorServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//Controller
//1.요청파라미터 조회
String op1 = request.getParameter("op1");
String op2 = request.getParameter("op2");
String operator = request.getParameter("operator");
//2. Business Logic (모델2는 비지니스 로직을 처리는 하되 클래스의 메소드를 호출해서 처리하는 식으로 처리한다.)
CalculatorService cs = new CalculatorService();
String resUrl = null;
try{
BigDecimal result = cs.calculate(op1, op2, operator);
request.setAttribute("result", result);
resUrl = "/model2/calc_result.jsp";
}catch(Exception e){
e.printStackTrace();
request.setAttribute("error_message", "관리자에게 문의하세요"+e.getMessage());
resUrl = "/model2/error.jsp";
}
//3. 응답
RequestDispatcher requestDispatcher = request.getRequestDispatcher(resUrl);
requestDispatcher.forward(request, response);
}
}
package calc.model;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
public class CalculatorService {
//비지니스 로직
public BigDecimal calculate(String op1, String op2, String operator)throws NumberFormatException{
//10/3->0.333333333333 이럴 시 소수점 처리하기 위해 BigDecimal사용
BigDecimal num1 = new BigDecimal(op1);
BigDecimal num2 = new BigDecimal(op2);
BigDecimal result = null;
//올림 : UP, CEIL, 내림 : DOWN, FLOOR, 반올림 : 5일경우 : HALF_EVEN, HALF_DOWN, HALF_UP
MathContext mc = new MathContext(3, RoundingMode.HALF_EVEN);//소수점 이하 몇자리, 반올림 처리
if(operator.equals("+")){
result = num1.add(num2, mc);
}else if(operator.equals("-")){
result = num1.subtract(num2, mc);
}else if(operator.equals("X")){
result = num1.multiply(num2, mc);
}else if(operator.equals("/")){
result = num1.divide(num2, mc);
}
return result;
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
계산결과 : ${requestScope.result }<br>
<a href="/myjsp/model2/calc_form.jsp">계산폼</a>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
오류가 발생했습니다. 관리자에게 문의하세요.<br>
오류내용 : ${requestScope.error_message }
</body>
</html>