forked from qangel/EECS-341-Project
-
Notifications
You must be signed in to change notification settings - Fork 1
/
login.jsp
149 lines (143 loc) · 4.15 KB
/
login.jsp
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
<%@page language="java" import="java.sql.*"%>
<%
String message = null; // Error message to display to user.
String redirect = null; // Where to send the user.
boolean hasCookie = false;
Cookie cookie = null;
String user = request.getParameter("uname");
String password = request.getParameter("pword");
/* Initialize the sql connection. */
Driver drs = (Driver)Class.forName("org.gjt.mm.mysql.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/jlj",
"jlj","fanball");
String command = "{call isUser(?,?)}";
CallableStatement cs = conn.prepareCall(command);
ResultSet rs = null;
/* Try to parse the user's cookies */
Cookie[] cookies = request.getCookies();
if(cookies != null) {
for(int i=0; i<cookies.length; i++) {
cookie = cookies[i];
if(cookie.getName().equals("uname"))
if(cookie.getValue() != "") {
user = cookie.getValue();
hasCookie = true;
}
if(cookie.getName().equals("password"))
if(cookie.getValue() != "") {
password = cookie.getValue();
hasCookie = true;
}
}
}
/* Check whether uname/password combination is in database */
if(hasCookie && (user!=null) && !user.equals("")
&& (password!=null) && !password.equals("")) {
cs.setString(1, user);
cs.setString(2, password);
rs = cs.executeQuery();
rs.first();
if(!rs.getString(1).equals("0")) {
if(user.equals("admin")) redirect = "admin.jsp";
else redirect = "user.jsp";
}
else {
cookie = new Cookie("uname", "");
response.addCookie(cookie);
cookie = new Cookie("password", "");
response.addCookie(cookie);
}
}
/* What to do if the user has entered a username/password combination */
if(!hasCookie) {
if((user!=null) && !user.equals("") && (password!=null)
&& !password.equals("")) {
/* Check the database for username/password combination. If so,
* store them as cookies and redirect to the site proper. */
cs.setString(1, user);
cs.setString(2, password);
rs = cs.executeQuery();
rs.first();
if(!rs.getString(1).equals("0")) {
if(user.equals("admin")) redirect = "admin.jsp";
else redirect = "user.jsp";
cookie = new Cookie("uname", user);
response.addCookie(cookie);
cookie = new Cookie("password", password);
response.addCookie(cookie);
}
else message = "Invalid username or password";
}
}
/* Display the login page. */
if (redirect == null) {
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css" />
<title>Fantasy Football</title>
</head>
<body>
<div align="center">
<form action="<%=request.getRequestURL()%>" method="post">
<table>
<tr>
<td align="center" colspan="2"><h3>Fantasy Football</h3></td>
</tr>
<%if(message != null) {%>
<tr>
<td align="center" colspan="2"><r><%= message %></r></td>
</tr>
<%}%>
<tr>
<td>Username:</td>
<td>
<%if(user == null) {%>
<input type="text" name="uname" size="60" style="width: 256px"/>
<%} else {%>
<input type="text" name="uname" value="<%= user %>"
size="60" style="width: 256px"/>
<%}%>
</td>
</tr>
<tr>
<td>Password:</td>
<td>
<input type="password" name="pword" size="60" style="width: 256px"/>
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Select" /></td>
</tr>
<tr>
<td align="center" colspan="2">
Not a member? <a href="register.jsp">register</a>
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
<%
} else {
/* Display a redirect page. */
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Refresh" content="5; URL=<%= redirect %>" />
<link rel="stylesheet" type="text/css" href="style.css" />
<title>Fantasy Football</title>
</head>
<body>
<div align="center">
Click <a href="<%= redirect %>">here</a> if your browser does not redirect
you.
</div>
</body>
</html>
<%}%>