-
Notifications
You must be signed in to change notification settings - Fork 183
Expand file tree
/
Copy pathAccountPageServlet.java
More file actions
185 lines (162 loc) · 7.26 KB
/
AccountPageServlet.java
File metadata and controls
185 lines (162 loc) · 7.26 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
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;
import javax.servlet.ServletException;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import org.bson.Document;
import static com.mongodb.client.model.Filters.*;
/**
* Java servlet for displaying and editing the account page.
*
* @author Joseph Eichenhofer, Emma He
*/
public class AccountPageServlet extends HttpServlet {
/*
* (non-Javadoc)
*
* @see
* javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
* javax.servlet.http.HttpServletResponse)
*
* Retrieve the session from HttpServletRequest.
* Get username from session to identify viewer's identity,
* and thisUsername from Request to identify whose account page is displaying.
* If this viewer's identity is the same as the account page,
* allow the viewer to edit the account.
* Otherwise, the viewer can only view the page without editing.
*
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
// Get session information
HttpSession session = req.getSession(false);
// if no session ,go to welcome page; otherwise, write out the account page.
if(session == null){
res.sendRedirect("/welcome");
} else {
// Get viewer's name from session, and user of the account page from request
// then check if this page belongs to the viewer
String username = (String) session.getAttribute("username");
String thisUsername = req.getParameter("username");
String pageUsername = username;
if(thisUsername != null && !thisUsername.equals(username)){
pageUsername = thisUsername;
}
// Set up the response content
PrintWriter content = res.getWriter();
res.setContentType("text/html; charset=htf-8");
res.setStatus(HttpServletResponse.SC_OK);
// Header ot eh HTML page, declare the title and css files
content.println("<!DOCTYPE html>");
content.println("<html>");
content.println("<head>");
content.println("<title>Account Page</title>");
content.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"AccountPage.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.print("<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("This is ");
content.println(pageUsername + "'s Page!");
content.println("</h1>");
// Display points of this account
content.println("<div class=\"wrapper\">");
content.println("<div class=\"container short\">");
content.println("<h2 class=\"header\">" + pageUsername + "'s Credits</h2>");
content.println("<h3 class=\"oneLine\">");
content.println(Database.getPoints(pageUsername) + " Credits</h3>");
content.println("</div>");
// Display the profile content of this account
content.println("<div class=\"container long\">");
content.println("<h2 class=\"header\">" + pageUsername + "'s Profile</h2>");
String profile = Database.getProfile(pageUsername);
if(profile == null){
profile = "";
}
content.println("<div class=\"sevralLine\" id=\"profile\" >" + profile + "</div>");
if(thisUsername == null || thisUsername.equals(username)){
content.println("<textarea placeholder=\"Change Your Profile\" class=\"inputField\">" + profile + "</textarea>");
content.println("<button id=\"submit\">Submit</button>");
content.println("<button id=\"edit\">Edit</button>");
}
content.println("</div>");
// Define the js file, and close tags for this page
content.println("<script type=\"text/javascript\" src=\"./Account.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, proceed updating data in account page.
if(session == null){
res.sendRedirect("/welcome");
} else {
// get the username from session
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();
// Get the data from request
BufferedReader reader = req.getReader();
StringBuilder sb = new StringBuilder();
String line = reader.readLine();
while(line != null){
sb.append(line + "\n");
line = reader.readLine();
}
reader.close();
String params = sb.toString();
// Parse the data into two parameters: type and value
String value = params.substring("value=".length(), params.length());
// If data is added successfully, response with success message.
// Otherwise, response with failure message.
boolean result = false;
result = Database.addAccountInfo(username, value, null, null);
if(result){
out.print("Success!");
}else {
out.print("Fail!");
}
}
}
}