2013년 7월 10일 수요일

(130710) 13일차 Cal.jsp, Cal2.jsp (request 객체를 이용한 계산기 프로그램)

 - Cal.jsp 소스
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!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=EUC-KR">
<title> 계산기 </title>
</head>
<body>
<h2> 계산기 </h2>
<form action = "Cal2.jsp" method = "post">
<table border = 2>
<tr>
<td> 피연산자1 </td>
<td> <input type = "text" name = "num1"> </td>
<td> 연산자 </td>
<td><select name="sign">
          <option value = "+"> + </option>
          <option value = "-"> - </option>
      <option value = "*"> * </option>
          <option value = "/"> / </option>
</select> </td>
<td> 피연산자2 </td>
<td> <input type = "text" name = "num2"> </td>
<td> <input type="submit" value="결과확인"> </td>
</tr>
</table>
</form>
</body>
</html>


 - 결과











 - Cal2.jsp 소스
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!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=EUC-KR">
<title> 계산기 </title>
</head>
<body>
<h2> 정답 </h2>
<%
int result = 0;
// 한글처리
request.setCharacterEncoding("euc-kr");
// 피연산자1
String num1 = request.getParameter("num1");
// 연산자
String sign = request.getParameter("sign");
  // 피연산자2
String num2 = request.getParameter("num2");
 
  if(sign.equals("+"))
  result = Integer.parseInt(num1) + Integer.parseInt(num2);
  if(sign.equals("-"))
  result = Integer.parseInt(num1) - Integer.parseInt(num2);
  if(sign.equals("*"))
  result = Integer.parseInt(num1) * Integer.parseInt(num2);
  if(sign.equals("/"))
  result = Integer.parseInt(num1) / Integer.parseInt(num2);
 
  out.println("<table border = '2'>");
  out.println("<tr>" + "<td>" + "정답 : " + "</td>" + "<td>" + result + "</td>" + "</tr>" + "</table>");
  out.println("<form action = 'Cal.jsp' method = 'post'>" + "<input type='submit' value='이전'>" + "</form>");
    %>
    
</body>
</html>


 - 결과