-
Notifications
You must be signed in to change notification settings - Fork 0
/
AccountImpl.java
162 lines (143 loc) · 4.32 KB
/
AccountImpl.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import java.io.*;
import java.rmi.server.UID;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
class Account
{
String username;
String password;
String type;
Account(String username,String password,String type)
{
this.username=username;
this.password=password;
this.type=type;
}
String getPassword()
{
return this.password;
}
String getType()
{
return this.type;
}
}
class Login{
String usernameEntered;
String password;
Login(String username,String password)
{
this.usernameEntered=username;
this.password=password;
}
public Account verifyCredentials()
{
final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
final String DB_URL="jdbc:mysql://localhost:3306/SSNEventPortal";
final String USER = "root";
final String PASS = "Mysql*07";
try {
Class.forName(JDBC_DRIVER);
Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
Statement stmt = conn.createStatement();
String sql;
sql = "SELECT * FROM Account WHERE username = '"+this.usernameEntered+"' and password= '"+this.password+"'";
ResultSet rs = stmt.executeQuery(sql);
if(rs.next()) {
String username=rs.getString("username");
String password=rs.getString("password");
String type=rs.getString("type");
Account a=new Account(username,password,type);
rs.close();
stmt.close();
conn.close();
return a;
}
else{
rs.close();
stmt.close();
conn.close();
return null;
}
}
catch(SQLException se) {
se.printStackTrace();
}
catch(Exception e) {
e.printStackTrace();
}
finally {
}
return null;
}
public int getUserID(String username)
{
final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
final String DB_URL="jdbc:mysql://localhost:3306/SSNEventPortal";
final String USER = "root";
final String PASS = "Mysql*07";
try {
Class.forName(JDBC_DRIVER);
Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
Statement stmt = conn.createStatement();
String sql;
sql = "SELECT UID FROM User WHERE username = '"+username+"'";
ResultSet rs = stmt.executeQuery(sql);
if(rs.next()) {
int userID=rs.getInt("UID");
rs.close();
stmt.close();
conn.close();
return userID;
}
else{
rs.close();
stmt.close();
conn.close();
return -1;
}
}
catch(SQLException se) {
se.printStackTrace();
}
catch(Exception e) {
e.printStackTrace();
}
finally {
}
return -1;
}
}
public class AccountImpl extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Database Result";
String docType =
"<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
String uname=request.getParameter("username");
String pwd= request.getParameter("password");
Login a=new Login(uname,pwd);
Account acnt=a.verifyCredentials();
if(acnt==null)
{
RequestDispatcher ds = request.getRequestDispatcher("login.html");
ds.include(request, response);
out.println("<br>Either username or password is invalid!");
// response.sendRedirect("index.html");
}
else{
int UID=a.getUserID(uname);
HttpSession session=request.getSession();
session.setAttribute("userID",UID);
session.setAttribute("type",acnt.type);
// out.println("Logged In");
RequestDispatcher ds = request.getRequestDispatcher("Dashboard");
ds.forward(request, response);
// response.sendRedirect("http://localhost/MiniProject/Dashboard");
}
}
}