Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix and update some backend feature #72

Merged
merged 6 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions init/full.sql
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ go

create table Customer (
customer_id int identity(1,1) not null primary key,
customer_firstname nvarchar(200) not null,
customer_lastname nvarchar(200) not null,
customer_firstname nvarchar(200) null,
customer_lastname nvarchar(200) null,
customer_gender nvarchar(5) null,
customer_phone varchar(11) null,
customer_address nvarchar(1000) null
Expand Down
279 changes: 140 additions & 139 deletions src/main/java/Controllers/LoginController.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,151 +26,152 @@
*/
public class LoginController extends HttpServlet {

/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try ( PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Login</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet Login at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try ( PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Login</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet Login at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
}
}
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.getRequestDispatcher("/index.jsp").forward(request, response);
}

/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String contextPath = request.getContextPath();
if (request.getParameter("btnSubmit") != null
&& ((String) request.getParameter("btnSubmit")).equals("Submit")) {
String email = request.getParameter("txtEmail");
String password = (String) request.getAttribute("txtPassword");
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String contextPath = request.getContextPath();
if (request.getParameter("btnSubmit") != null
&& ((String) request.getParameter("btnSubmit")).equals("Submit")) {
String email = request.getParameter("txtEmail");
String password = (String) request.getAttribute("txtPassword");

Account account = new Account(email, password);
AccountDAO dao = new AccountDAO();
boolean success;
try {
success = dao.login(account);
} catch (SQLException ex) {
Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, null, ex);
success = false;
}
// Truy xuất URL hiện tại từ session attribute
HttpSession session = request.getSession();
String previousUrl = (String) session.getAttribute("previousUrl");
if (success) {
account = dao.getAccount(email);
String accountType = account.getAccountType();
boolean isRemembered = (request.getParameter("chkRememberMe") != null
&& request.getParameter("chkRememberMe").equals("remember"));
if (isRemembered) {
if (accountType.equals("user")) {
int cAge = 24 * 60 * 60 * 7; // 7 days
account = dao.getAccount(email);
String username = account.getUsername();
username = URLEncoder.encode(username, "UTF-8");
int userID = account.getAccountID();
Cookie cUser = new Cookie("user", username);
cUser.setMaxAge(cAge);
cUser.setPath("/");
response.addCookie(cUser);
Cookie cUserID = new Cookie("userID", String.valueOf(userID));
cUser.setMaxAge(cAge);
cUser.setPath("/");
response.addCookie(cUserID);
if (previousUrl != null) {
// Chuyển hướng người dùng về trang hiện tại
response.sendRedirect(previousUrl);
} else {
// Nếu không có URL trước đó, chuyển hướng người dùng về trang mặc định
response.sendRedirect("/");
Account account = new Account(email, password);
AccountDAO dao = new AccountDAO();
boolean success;
try {
success = dao.login(account);
} catch (SQLException ex) {
Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, null, ex);
success = false;
}
} else {
int cAge = 24 * 60 * 60 * 7; // 7 days
account = dao.getAccount(email);
String username = account.getUsername();
username = URLEncoder.encode(username, "UTF-8");
Cookie adminCookie = new Cookie("admin", username);
adminCookie.setMaxAge(cAge);
adminCookie.setPath("/");
response.addCookie(adminCookie);
response.sendRedirect("/admin");
}
} else {
if (accountType.equals("user")) {
account = dao.getAccount(email);
String username = account.getUsername();
int userID = account.getAccountID();
session = request.getSession();
session.setAttribute("user", username);
session.setAttribute("userID", userID);
if (previousUrl != null) {
// Chuyển hướng người dùng về trang hiện tại
response.sendRedirect(previousUrl);
} else {
// Nếu không có URL trước đó, chuyển hướng người dùng về trang mặc định
response.sendRedirect("/");
// Truy xuất URL hiện tại từ session attribute
HttpSession session = request.getSession();
String previousUrl = (String) session.getAttribute("previousUrl");
if (success) {
account = dao.getAccount(email);
String accountType = account.getAccountType();
boolean isRemembered = (request.getParameter("chkRememberMe") != null
&& request.getParameter("chkRememberMe").equals("remember"));
if (isRemembered) {
if (accountType.equals("user")) {
int cAge = 24 * 60 * 60 * 7; // 7 days
account = dao.getAccount(email);
String username = account.getUsername();
username = URLEncoder.encode(username, "UTF-8");
int userID = account.getAccountID();
Cookie cUser = new Cookie("user", username);
cUser.setMaxAge(cAge);
cUser.setPath("/");
response.addCookie(cUser);
Cookie cUserID = new Cookie("userID", String.valueOf(userID));
cUser.setMaxAge(cAge);
cUser.setPath("/");
response.addCookie(cUserID);
if (previousUrl != null) {
// Chuyển hướng người dùng về trang hiện tại
response.sendRedirect(previousUrl);
} else {
// Nếu không có URL trước đó, chuyển hướng người dùng về trang mặc định
response.sendRedirect("/");
}
} else {
int cAge = 24 * 60 * 60 * 7; // 7 days
account = dao.getAccount(email);
String username = account.getUsername();
username = URLEncoder.encode(username, "UTF-8");
Cookie adminCookie = new Cookie("admin", username);
adminCookie.setMaxAge(cAge);
adminCookie.setPath("/");
response.addCookie(adminCookie);
response.sendRedirect("/admin");
}
} else {
if (accountType.equals("user")) {
account = dao.getAccount(email);
String username = account.getUsername();
int userID = account.getAccountID();
session = request.getSession();
session.setAttribute("user", username);
session.setAttribute("userID", userID);
if (previousUrl != null) {
// Chuyển hướng người dùng về trang hiện tại
response.sendRedirect(previousUrl);
} else {
// Nếu không có URL trước đó, chuyển hướng người dùng về trang mặc định
response.sendRedirect("/");
}
} else if (accountType.equals("admin")) {
account = dao.getAccount(email);
String username = account.getUsername();
session = request.getSession();
session.setAttribute("admin", username);
response.sendRedirect("/admin");
} else if (accountType.equals("staff")) {
account = dao.getAccount(email);
String username = account.getUsername();
session = request.getSession();
session.setAttribute("admin", username);
response.sendRedirect("/staff");
} else if (accountType.equals("staff")) {
account = dao.getAccount(email);
String username = account.getUsername();
session = request.getSession();
session.setAttribute("promotionManager", username);
response.sendRedirect("/promotionManager");
} else {
response.sendRedirect("/home#failure_login");
}
}
}
} else {
account = dao.getAccount(email);
String username = account.getUsername();
session = request.getSession();
session.setAttribute("admin", username);
response.sendRedirect("/admin");
}
}
} else {
response.sendRedirect("/home#failure_login");
}
}
}

/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>

}
Loading