forked from sectoolstutorial/Wisclick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RankListPageServlet.java
95 lines (82 loc) · 3.64 KB
/
RankListPageServlet.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
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletException;
/**
* Java servlet for displaying the ranking data retrieved from database.
*
* @author Emma He
*/
public class RankListPageServlet extends HttpServlet {
/*
* (non-Javadoc)
*
* @see
* javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
* javax.servlet.http.HttpServletResponse)
*
* Retrieve the session from HttpServletRequest.
* Retrieve the data from the database.
* Display the top five ranking on the page.
*
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
HttpSession session = req.getSession(false);
if(session == null){
res.sendRedirect("/welcome");
}
else {
// Set up the response content
PrintWriter content = res.getWriter();
res.setContentType("text/html; charset=htf-8");
res.setStatus(HttpServletResponse.SC_OK);
String username = (String) session.getAttribute("username");
// Get the data from the database
ArrayList<String[]> rankList = Database.getRankList();
// Header of the HTML page, declares the title and css files
content.println("<!DOCTYPE html>");
content.println("<html>");
content.println("<head>");
content.println("<title>Ranking Page</title>");
content.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"./RankList.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\">");
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 and the ranking list
content.println("<h1> Ranking </h1>");
content.println("<div id=\"container\">");
content.println("<ul>");
for(int i = 0; i < rankList.size(); i++){
content.println("<li>");
content.println("<span class=\"rankingNum\">" + (i+1) + "</span>");
content.println("<a href=\"account?username=" + rankList.get(i)[0] + "\">" + rankList.get(i)[0] + "</a>");
content.println("<span class=\"rankingCredits\">" + rankList.get(i)[1] + "</span>");
content.println("</li>");
}
content.println("</ul>");
content.println("</div>");
// Close the tags
content.println("</body>");
content.println("</html>");
}
}
}