[calculator.html]

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
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="EUC-KR">
<title>계산기 실습</title>
</head>
<body>
<CENTER>
    <h2>계산기</h2>
    <hr width="100%">
    <form name = "calculator" action="./result" method=post>
        <input type="text" name = "num1" size=5>
        <select name = "getOperator">
            <option value="+">+</option>
            <option value="-">-</option>
            <option value="*">*</option>
            <option value="/">/</option>
        </select>
        <input type="text" name = "num2"  size=5>
        <input type="submit" value="계산" name = "resultbtn">
        <input type="reset" value="다시입력" name = "resetbtn">
    </form>
    </CENTER>
</body>
</html>
 
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
 

 

 

[JavaResources > Calc.java]

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import javax.servlet.*;
import java.io.*;
 
public class CalcServlet extends HttpServlet{
     public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{
             doPost(request,response);        //post 방식으로 데이터 전송
     }
     
     public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException{
        int num1, num2;
        int result;
        String op;
        
        //클라이언트 응답시 전달될 컨텐츠에 대한 기본 타입 설정
        response.setContentType("text/html");
        response.setCharacterEncoding("euc-kr");
        
        //클라이언트 응답을 위한 출력 스트릠 확보
        PrintWriter out = response.getWriter();
         
        num1 = Integer.parseInt(request.getParameter("num1"));    //num1로 되어 있는 값 int타입으로 가져와 저장
        num2 = Integer.parseInt(request.getParameter("num2"));    //num2로 되어 있는 값 int타입으로 가져와 저장
        op = request.getParameter("getOperator");                //연산자 받아 저장
        
        //result = calc(num1, num2, op);                        //메소드 호출
        Calc calc = new Calc(num1, num2, op);    
        result = calc.getResult();
        
        //html write
        out.write("<HTML><HEAD><TITLE>ServletTest</TITLE></HEAD>");
        out.write("<BODY><CENTER><H2>");
        out.write("계산결과</H2>");
        out.write("<hr width='100%'>");
        out.write(num1+" "+op+" "+num2+" = "+result);
        out.write("</CENTER></BODY></HTML>");
        out.close();
        
     }
 
    private int calc(int num1, int num2, String op) {
        int str=0;
        
        switch (op) {
        
            case "+":
                str = num1+num2;
                break;
            case "-":
                str = num1-num2;
                break;
            case "*":
                str = num1*num2;
                break;
            case "/":
                str = num1/num2;
                break;
                
            default:
                break;
        }
        return str;
    }
     
}
 
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
 

 

[web.xml]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="UTF-8"?>
id="WebApp_ID" version="4.0">
  <display-name>Calc</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>CalcServlet</display-name>
    <servlet-name>CalcServlet</servlet-name>
    <servlet-class>CalcServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>CalcServlet</servlet-name>
    <url-pattern>/result</url-pattern>
  </servlet-mapping>
</web-app>
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
 

'Servlet & JSP' 카테고리의 다른 글

JSP 예제_page  (0) 2019.06.20
JSP 주석  (0) 2019.06.20
JSP 예제_현재 시간 출력  (0) 2019.06.20
Servlet 예제_현재 시간 출력  (0) 2019.06.20
Servlet 구현 환경  (0) 2019.06.20

+ Recent posts