package servlet; import cart.applicationBeans.CategoryBean; import cart.applicationBeans.ItemBean; import cart.applicationBeans.ItemCategoryBean; import cart.applicationBeans.ItemInventoryBean; import cart.dbUtils.DBConnection; import cart.dbUtils.DBConnectionPool; import cart.screenWranglers.BaseScreenWrangler; import cart.screenWranglers.MainScreenWrangler; import java.io.*; import java.util.HashMap; import javax.servlet.*; import javax.servlet.http.*; /* ControllerServlet - controls all processing * and holds all applicationBeans * author - Lawrence Truett - FluffyCat.com * date - May 22, 2003 - San Diego, CA **/ public final class ControllerServlet extends HttpServlet { private CategoryBean categoryBean; private ItemBean itemBean; private ItemCategoryBean itemCategoryBean; private ItemInventoryBean itemInventoryBean; private DBConnectionPool dbConnectionPool; private ServletContext servletContext; /** Initializes the servlet. */ public void init(ServletConfig config) throws ServletException { super.init(config); servletContext = getServletContext(); dbConnectionPool = new DBConnectionPool(); DBConnection dbc = dbConnectionPool.getConnection(); servletContext.setAttribute("dbConnectionPool", dbConnectionPool); categoryBean = new CategoryBean(dbc); servletContext.setAttribute("categoryBean", categoryBean); itemBean = new ItemBean(dbc); servletContext.setAttribute("itemBean", itemBean); itemCategoryBean = new ItemCategoryBean(dbc); servletContext.setAttribute("itemCategoryBean", itemCategoryBean); itemInventoryBean = new ItemInventoryBean(dbc); servletContext.setAttribute("itemInventoryBean", itemInventoryBean); dbConnectionPool.returnConnection(dbc); } /** Destroys the servlet. */ public void destroy() { super.destroy(); } /** Processes requests for both HTTP GET * and POST methods. * @param request servlet request * @param response servlet response */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession httpSession = request.getSession(); Boolean stringSignedIn = (Boolean)httpSession.getAttribute("signedIn"); if (null == stringSignedIn) { httpSession.setAttribute("signedIn", new Boolean(false)); } BaseScreenWrangler wrangler; wrangler = (BaseScreenWrangler) httpSession.getAttribute("wrangler"); if (null == wrangler) { wrangler = new MainScreenWrangler(); } wrangler.wrangleScreen(request, servletContext); httpSession.setAttribute("wrangler", wrangler.getNextWrangler()); RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher( wrangler.getNextWrangler().getNextScreen()); requestDispatcher.forward(request, response); } /** Handles the HTTP GET method. * @param request servlet request * @param response servlet response */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** Handles the HTTP POST method. * @param request servlet request * @param response servlet response */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** Returns a short description of the servlet. */ public String getServletInfo() { String message = "Controller Servlet for Shopping Cart Application, " + "Copyright Lawrence Truett 2003-2004, all rights reserved."; return message; } }