-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStatsPageServlet.java
More file actions
34 lines (26 loc) · 1001 Bytes
/
StatsPageServlet.java
File metadata and controls
34 lines (26 loc) · 1001 Bytes
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
package com.google.codeu.servlets;
import com.google.codeu.data.Datastore;
import com.google.gson.JsonObject;
import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/** Responds with a hard-coded message for testing purposes. */
@WebServlet("/stats")
public class StatsPageServlet extends HttpServlet {
private Datastore datastore;
@Override
public void init() {
datastore = new Datastore();
}
/** Responds with site statistics in JSON. */
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("application/json");
int messageCount = datastore.getTotalMessageCount();
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("messageCount", messageCount);
response.getOutputStream().println(jsonObject.toString());
}
}