본문 바로가기
back-end/서블릿

서블릿 이용 계산기(저장용)

by Ryuuu 2021. 6. 29.

JSP를 학습하기 전 서블릿만 이용해서 계산기를 만들어 보았다.

매우 무식한 방법일지도 모르나 이렇게 하면 JSP사용하는 이유를 나중에 더 알 수 있대서 이렇게 만들었다.

백스페이스나 몇개 귀찮아서 기능 구현 안한거도 있긴하다.

그냥 지울라면 C나 CE누르면 됨

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.ryuuu.web;
 
import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
import org.graalvm.polyglot.Context;
@WebServlet("/calc_page")
public class calcpage extends HttpServlet{ 
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse rsp) throws ServletException, IOException {
        Cookie[] cookies = req.getCookies();
        PrintWriter out = rsp.getWriter();
         
        //입력 인자들 가지고옴
        String value = req.getParameter("value");
        String op = req.getParameter("oper");
        String dot = req.getParameter("dot");
         
        String exp = "";
         
        //쿠키에 저장한 계산식 가지고옴
        if(cookies != null) {
            for (Cookie c : cookies) {
                if(c.getName().equals("exp")) {
                    exp = c.getValue();
                    break;
                }
            }
        }
         
        // = 누르면 계산
        if(op != null && op.equals("=")) {
            try (Context context = Context.create("js")) {
                exp = String.valueOf(context.eval("js",exp));
            } catch (Exception e) {
                System.err.println();
            }
        }
         
        // = 아니면 계산식에 이어줌
        else {
            exp += (value == null)? "" : value;
            exp += (op == null)? "" : op;
            exp += (dot == null)? "" : dot;
        }
        Cookie expCookie = new Cookie("exp", exp);
        if(op != null &&(op.equals("CE") || op.equals("C"))) {
            expCookie.setMaxAge(0);
        }
        rsp.addCookie(expCookie);
        rsp.sendRedirect("calc_page");
    }
 
@Override
    protected void doGet(HttpServletRequest req, HttpServletResponse rsp) throws ServletException, IOException {
    Cookie[] cookies = req.getCookies();
    String exp = "0";
    //계산식 쿠키 가지고옴
    if(cookies != null) {
        for (Cookie c : cookies) {
            if(c.getName().equals("exp")) {
                exp = c.getValue();
                break;
            }
        }
    }
     
    PrintWriter out = rsp.getWriter();
     
    out.write("<!DOCTYPE html>");
    out.write("<html>");
    out.write("<head>");
    out.write("<meta charset=\"EUC-KR\">");
    out.write("<title>calc</title>");
    out.write("<style>");
    out.write("input {");
    out.write(" width:50px;");
    out.write("height:50px");
    out.write("}");
    out.write(".output{");
    out.write("height: 50px;");
    out.write("background: #e9e9e9;");
    out.write("font-size:24px;");
    out.write("font-weight:bold;");
    out.write("text-align: right;");
    out.write("padding: 0px, 5px;");
         
    out.write("}");
    out.write("</style>");
    out.write("</head>");
    out.write("<body>");
    out.write("<form method=\"post\">");
    out.write("<table>");
    out.write("<tr>");
    out.printf("<td class = \"output\" colspan=\"4\">%s</td>",exp);    
    out.write("</tr>");
    out.write("<tr>");
    out.write("<td><input type=\"submit\" name = \"oper\" value=\"CE\"></td>");
    out.write("<td><input type=\"submit\" name = \"oper\" value=\"C\"></td>");
    out.write("<td><input type=\"submit\" name = \"oper\" value=\"BS\"></td>");
    out.write("<td><input type=\"submit\" name = \"oper\" value=\"/\"></td>");
    out.write("</tr>");
    out.write("<tr>");
    out.write("<td><input type=\"submit\" name = \"value\" value=\"7\"></td>");
    out.write("<td><input type=\"submit\" name = \"value\" value=\"8\"></td>");
    out.write("<td><input type=\"submit\" name = \"value\" value=\"9\"></td>");
    out.write("<td><input type=\"submit\" name = \"oper\" value=\"*\"></td>");
    out.write("</tr>");
    out.write("<tr>");
    out.write("<td><input type=\"submit\" name = \"value\" value=\"4\"></td>");
    out.write("<td><input type=\"submit\" name = \"value\" value=\"5\"></td>");
    out.write("<td><input type=\"submit\" name = \"value\" value=\"6\"></td>");
    out.write("<td><input type=\"submit\" name = \"oper\" value=\"-\"></td>");
    out.write("</tr>");
    out.write("<tr>");
    out.write("<td><input type=\"submit\" name = \"value\" value=\"1\"></td>");
    out.write("<td><input type=\"submit\" name = \"value\" value=\"2\"></td>");
    out.write("<td><input type=\"submit\" name = \"value\" value=\"3\"></td>");
    out.write("<td><input type=\"submit\" name = \"oper\" value=\"+\"></td>");
    out.write("</tr>");
    out.write("<tr>");
    out.write("<td></td>");
    out.write("<td><input type=\"submit\" name = \"value\" value=\"0\"></td>");
    out.write("<td><input type=\"submit\" name = \"dot\" value=\".\"></td>");
    out.write("<td><input type=\"submit\" name = \"oper\" value=\"=\"></td>");
    out.write("</tr>");
    out.write("</table>");
    out.write("</form>");
    out.write("</body>");
    out.write("</html>");
     
    }
     
}

'back-end > 서블릿' 카테고리의 다른 글

GET/POST 처리 서비스함수  (0) 2021.06.29
JSP COOKIE  (0) 2021.06.29
JSP SESSION 메소드  (0) 2021.06.29
JSP SESSION 객체(application과 차이점)  (0) 2021.06.29
JSP application 객체  (0) 2021.06.29

댓글