forked from sectoolstutorial/Wisclick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEarnPointsPageServlet.java
142 lines (122 loc) · 5.66 KB
/
EarnPointsPageServlet.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
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
import java.io.IOException;
import java.io.PrintWriter;
import java.io.BufferedReader;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Java servlet for displaying the earn points page and handle the points change.
*
* @author Emma He
*/
public class EarnPointsPageServlet extends HttpServlet {
/*
* (non-Javadoc)
*
* @see
* javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
* javax.servlet.http.HttpServletResponse)
*
* Retrieve the session from HttpServletRequest.
* Display the earn points page for users to
* click the button to earn points,
* and add points to their own account.
*
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
// Get session information
HttpSession session = req.getSession(false);
// if no session, go to welcome page; otherwise, write out earn points page
if(session == null){
res.sendRedirect("/welcome");
} else {
// Get the username from session
String username = (String) session.getAttribute("username");
// Set up the response content
PrintWriter content = res.getWriter();
res.setContentType("text/html; charset+utf-8");
res.setStatus(HttpServletResponse.SC_OK);
// Header of the HTML page, declares the title and css files
content.println("<!DOCTYPE>");
content.println("<html>");
content.println("<head>");
content.println("<title>Earn Credits Page</title>");
content.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"EarnPoints.css\">");
content.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"Navbar.css\">");
content.println("</head>");
// Body of the HTML page
content.println("<body>");
// Logo, course name, and navigation bar
content.println("<div id=\"headerNav\">");
content.println("<img src=\"./color-flush-reverse-UWlogo-print.png\" width=\"270\" height=\"90\" class=\"d-inline-block align-top\" alt=\"\">");
content.println("<span class=\"hello\">Logged in as "+ username +"</span>");
content.println("<div id=\"stripe\">");
content.println("<form action=\"logout\" method=\"POST\" class=\"logoutForm\"");
content.println("accept-charset=\"utf-8\">");
content.print("<a class=\"nav\" href=\"account\">My Account</a>");
content.print("<a class=\"nav\" href=\"earn\">Earn Credits</a>");
content.print("<a class=\"nav\" href=\"transfer\">Transfer Credits</a>");
content.print("<a class=\"nav\" href=\"rank\">Ranking</a>");
content.print("<input value=\"Log Out\" type=\"submit\" class=\"logoutInput nav\"></form>");
content.println("</div></div>");
// Headline
content.println("<h1>");
content.println("Welcome, " + username + "!");
content.println("</h1>");
// Display the button-clicking game and allow user to add points earned
content.println("<div id=\"container\">");
content.println("<div class=\"header\"><h2>Click to Earn</h2></div>");
content.println("<div class=\"line\" id=\"message\"></div>");
content.println("<div class=\"line point\"><span id=\"click\">" + Database.getPoints(username) + "</span> Credits</div>");
content.println("<div class=\"line\"></div>");
content.println("<div class=\"line\">");
content.println("<button class=\"buttonLike\">CLICK ME</button>");
content.println("</div></div>");
// Declare the js file, and then close the tags
content.println("<script type=\"text/javascript\" src=\"EarnPoints.js\"></script>");
content.println("</body>");
content.println("</html>");
}
}
/*
* (non-Javadoc)
*
* @see
* javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
* javax.servlet.http.HttpServletResponse)
*
* Retrieve the session from HttpServletRequest.
* Retrieve the data from XMLHttpRequest.
* Add the data to the database, and send a message with response.
*
*/
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {
// Get session information
HttpSession session = req.getSession(false);
// if no session, go to the welcome page, otherwise, proceeding update data in earn points page
if (session != null) {
// Get the username from sessiion
String username = (String) session.getAttribute("username");
// Set up the response content
res.setContentType("text/html; charset=utf-8");
res.setStatus(HttpServletResponse.SC_OK);
res.setHeader("Cache-Control", "no-cache");
res.setHeader("Pragma", "no-cache");
PrintWriter out = res.getWriter();
Boolean result = Database.setPoints(username, new Integer("1"));
// If data is added successfully, response with success message.
// Otherwise, response with failure message.
if(result){
out.print("Success!");
}
else{
out.print("Fail!");
}
} else {
res.sendRedirect("/welcome");
}
}
}