New paste Repaste Download
AdminController:
package com.controller;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.dao.AdminDAO;
import com.dao.ProductDAO;
import com.entity.Admin;
import com.entity.Product;
import com.entity.Customer;
@WebServlet(urlPatterns = { "/AdminController", "/admin" })
public class AdminController extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private AdminDAO adminDAO;
    // No-argument constructor
    public AdminController() {
        super();
    }
    // Initialize the CustomerDAO object
    @Override
    public void init() throws ServletException {
        this.adminDAO = new AdminDAO(); // Assuming your AdminDAO has a no-argument constructor
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");
        try {
            if (action.equalsIgnoreCase("login")) {
                String adminId = request.getParameter("adminID");
                int converted_admin_id = Integer.parseInt(adminId);
                String password = request.getParameter("password");
                Admin admin = this.adminDAO.get(new Admin(converted_admin_id, password));
                if (admin != null) {
                    HttpSession session = request.getSession();
                    session.setAttribute("admin", admin);
                    session.setAttribute("role", "admin");
                    response.sendRedirect("AdminHome.jsp");
                
                } else {
                    request.setAttribute("error", "Invalid ID or Password");
                    // Correct path to your login page
                    request.getRequestDispatcher("AdminLogin.jsp").forward(request, response);
                }
            }else if(action.equalsIgnoreCase("DeleteCustomer")){
            
             String customerId = request.getParameter("customerId");
                int converted_customer_id = Integer.parseInt(customerId);
                
                boolean isDeleted = adminDAO.DeleteCustomer(converted_customer_id);
                
                if (isDeleted) {
                    request.getRequestDispatcher("/CustomerManagement.jsp").forward(request, response);
                } else {
                    request.setAttribute("error", "Deletion failed.");
                    request.getRequestDispatcher("/CustomerManagement.jsp").forward(request, response);
                }
                
            }else if (action.equalsIgnoreCase("addProduct")) {
             // Product Addition logic
                try {
                    String productName = request.getParameter("productName");
                    String price = request.getParameter("price");
                    double converted_price = Double.parseDouble(price);
                    String description = request.getParameter("description");
                    String imgUrl = request.getParameter("imgUrl");
                    
                    Product product = new Product(productName, converted_price, description, imgUrl);
                    int isRegistered = adminDAO.addProduct(product);
                    if (isRegistered>0) {
                        request.getRequestDispatcher("/Product_Successfully_Added.jsp").forward(request, response);
                    } else {
                        request.setAttribute("error", "Product Addition failed.");
                        request.getRequestDispatcher("/ProductManagement.jsp").forward(request, response);
                    }
                }catch (Exception e) {
                    e.printStackTrace();
                    request.setAttribute("error", "An error occurred during Addition.");
                    request.getRequestDispatcher("/ProductManagement.jsp").forward(request, response);
                }
            }
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
            // Handle exception, navigate to error page
            request.setAttribute("error", "An unexpected error occurred.");
            request.getRequestDispatcher("errorPage.jsp").forward(request, response);
}
}
}
Filename: None. Size: 5kb. View raw, , hex, or download this file.

This paste expires on 2024-12-27 12:23:21.790899. Pasted through web.