Language/└JSP | Servlet

[JSP] getParameter()를 이용한 간단한 사칙연산 계산기 | Yoon's Dev

Yooniron 2021. 4. 14. 21:25

[JSP] getParameter()를 이용한 간단한 사칙연산 계산기

 

 


TO Do.

 

- 각각의 input 태그 안에 들어있는 피연산자와 연산자 두 개를 +, -, *, / 를 해서 보내기(submit)

- calcProc.jps 에 연산결과 출력하기


목표: calcProc.jsp 에 연산결과 출력!


1. calc.jsp

 

calc.jsp

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
    <h1>피연산자와 연산자를 입력하시오.</h1>
    <form action="calcProc.jsp" method="post">
        숫자1: <input type="text" name="num1"><br>
        숫자2: <input type="text" name="num2"><br>
        연산자: <select name="operator">
                <option value="+">+</option>
                <option value="-">-</option>
                <option value="*">*</option>
                <option value="/">/</option>
            </select>
        <br>
        <input type="submit" value="보내기">
    </form>
</body>
</html>
cs

 

<form> 태그의 action 값을 calcProc.jsp로 향하고 post 형식으로 넘겨주었다

 

파라미터

 

숫자 1: num1

숫자 2: num2

연산자: operator

 

위와 같이 피연산자와 연산자를 calcProc.jsp로 넘길 것이다.


 

2. calcProc.jsp

 

간단하게 40 + 20과 100 / 2 연산을 해보도록 하자.

calcProc.jsp // 40 + 20

 

calcProc.jsp // 10 / 2

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
    <h1>연산 결과</h1>
    <%
        request.setCharacterEncoding("EUC-KR"); //인코딩 형식
        
        int num1 = Integer.parseInt(request.getParameter("num1"));
        int num2 = Integer.parseInt(request.getParameter("num2"));
        
        String operator = request.getParameter("operator"); //select 태그의 name 값 operator에 저장
        
    %>
    <%
    //select 태그 option 값에 따라 사칙연산
    if (operator.equals("+")) { %>
           <%=num1%> + <%=num2%> = <%=num1+num2 %>
       <%
        } else if (operator.equals("-")) { %>
            <%=num1%> - <%=num2%> = <%=num1-num2 %>
       <%
        }else if (operator.equals("*")){ %>
            <%=num1%> * <%=num2%> = <%=num1*num2 %>
       <%
        }else{    %>
            <%=num1%> / <%=num2%> = <%=num1/num2 %>
       <%
        }
        %>
 
</body>
</html>
cs

이 코드에서 request.getParameter("calc.jsp에서 받아 오고 싶은 name 값");

 

원하는 변수에 저장해주면 된다.

저는 int num1과 num2, String operator에 저장하기로 했어요!

 

그러고 나서, calc.jsp의 select 태그 안에 값에 따라 계산이 되게 하려면 if 문으로 operator의 값이 일치하는지 비교해주면서 연산을 해주면 된다.!!

 

operator.equlas()로 비교해 주는 이유는 위에서 operator String 형식으로 저장해 주었기 때문이다.

 

 

 

 

40 + 20 연산결과
100 / 2 연산결과

 


 

 

3. 함께 보면 좋은 글

 

 

[JSP] getParameter()를 이용하여 구구단 출력하기 | Yoon's Dev

request.getParameter()를 이용하여 구구단 출력하기 TO Do. - gugudan.jsp에서 input 태그 안에 숫자에 따라 보내기(submit) - gugudanProc.jsp에서 해당하는 숫자에 따른 구구단 출력! 목표: gugudanProc.jsp에..

yooniron.tistory.com