diff --git a/init/full.sql b/init/full.sql
index 3fb5c108..7786b4c7 100644
--- a/init/full.sql
+++ b/init/full.sql
@@ -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
diff --git a/src/main/java/Controllers/LoginController.java b/src/main/java/Controllers/LoginController.java
index b0c1d49f..cec60fb9 100644
--- a/src/main/java/Controllers/LoginController.java
+++ b/src/main/java/Controllers/LoginController.java
@@ -26,151 +26,152 @@
*/
public class LoginController extends HttpServlet {
- /**
- * Processes requests for both HTTP GET
and POST
- * 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("");
- out.println("");
- out.println("
GET
and POST
+ * 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("");
+ out.println("");
+ out.println("");
+ out.println("GET
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);
- }
+ // GET
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 POST
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 POST
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";
- }//