-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTotalService.java
More file actions
51 lines (39 loc) · 1.5 KB
/
TotalService.java
File metadata and controls
51 lines (39 loc) · 1.5 KB
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
package services;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.CalcEngine;
@WebServlet("/total")
public class TotalService extends HttpServlet {
private static final long serialVersionUID = 1L;
public TotalService() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintStream out = new PrintStream(response.getOutputStream());
HttpSession session = request.getSession(true);
if (session.getAttribute("total") == null) {
session.setAttribute("total", "0");
}
Map<String, String[]> parameters = request.getParameterMap();
if (parameters.containsKey("a")) {
String a = request.getParameter("a");
String total = (String)session.getAttribute("total");
CalcEngine engine = CalcEngine.getInstance();
String newTotal = engine.compute("add", a, total);
session.setAttribute("total", newTotal);
out.println(newTotal);
} else {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}