-
Notifications
You must be signed in to change notification settings - Fork 183
Expand file tree
/
Copy pathViewPageServlet.java
More file actions
118 lines (103 loc) · 4.36 KB
/
ViewPageServlet.java
File metadata and controls
118 lines (103 loc) · 4.36 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
import java.io.IOException;
import java.io.PrintWriter;
import java.io.FileInputStream;
import java.io.BufferedReader;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletException;
/**
* Display either a welcome page or an account page.
*
* @author Joseph Eichenhofer
*/
public class ViewPageServlet extends HttpServlet {
/*
* (non-Javadoc)
*
* @see
* javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
* javax.servlet.http.HttpServletResponse)
*
* Serve one of two "pages" showing either a login form (if no session is
* registered with the request) or the account page that are associated
* with the session (if one exists).
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
PrintWriter content = res.getWriter();
HttpSession session = req.getSession(false);
String loginM = req.getParameter("loginMessage");
// set encoding/type
res.setContentType("text/html; charset=utf-8");
// set good status code
res.setStatus(HttpServletResponse.SC_OK);
if (session == null) {
// no session, show "login" form
content.println("<!DOCTYPE html>");
content.println("<html>");
content.println("<head>");
content.println("<title>Welcome Page</title>");
content.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"WelcomePage.css\">");
content.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"Navbar.css\">");
content.println("</head>");
content.println("<body>");
content.println("<img src=\"./color-flush-reverse-UWlogo-print.png\" width=\"270\" height=\"90\" class=\"d-inline-block align-top\" alt=\"\">");
content.println("<div id=\"container\">");
content.println("<h1>");
content.println("WisClick");
content.println("</h1>");
// write out form for setting username
content.println("<form action=\"welcome\" method=\"POST\"");
content.println("accept-charset=\"utf-8\">");
content.println("USERNAME: <input type=\"text\" name=\"username\">");
content.println("PASSWORD: <input type=\"password\" name=\"password\">");
if(loginM != null && loginM != ""){
content.println("<div>Wrong Username or Password</div>");
req.setAttribute("loginMessage", "");
}
content.println("<input type=\"submit\" value=\"Submit\">");
content.println("</form>");
content.println("</div>");
content.println("</body>");
content.println("</html>");
} else {
res.sendRedirect("/account");
}
}
/*
* (non-Javadoc)
*
* @see
* javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
* javax.servlet.http.HttpServletResponse)
*
* Retrieve username and password from HttpServletRequest.
* Authenticate the username and password
* if the username does not exits, create a new account
*
*/
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
if (username != null && !username.equals(new String(""))){
if(Database.authenticateUser(username, password)){
req.getSession(true).setAttribute("username", username);
req.getSession().setAttribute("clicks", new Integer(0));
// redirect to main page
res.sendRedirect("/account");}
else {
res.sendRedirect("/welcome?loginMessage=Wrong Username of Password");
}
}
else {
res.sendRedirect("/welcome");
}
}
}