Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @WebServlet(name = "ControllerServlet",
- loadOnStartup = 1,
- urlPatterns = {
- "/category",
- "/addToCart",
- "/viewCart",
- "/updateCart",
- "/checkout",
- "/purchase",
- "/chooseLanguage"})
- @EJB
- private CategoryFacade categoryFacade;
- @EJB
- private OrderManager orderManager;
- @Override
- public void init() throws ServletException {
- // store category list in servlet context
- getServletContext().setAttribute("categories", categoryFacade.findAll());
- }
- /**
- * 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 {
- String userPath = request.getServletPath();
- // if category page is requested
- if (userPath.equals("/category")) {
- // get categoryId from request
- String categoryId = request.getQueryString();
- if (categoryId != null) {
- // get selected category
- Category selectedCategory = categoryFacade.find(Short.parseShort(categoryId));
- // place selected category in request scope
- request.setAttribute("selectedCategory", selectedCategory);
- // get all products for selected category
- Collection<Product> categoryProducts = selectedCategory.getProductCollection();
- // place category products in request scope
- request.setAttribute("categoryProducts", categoryProducts);
- }
- // if cart page is requested
- } else if (userPath.equals("/viewCart")) {
- // TODO: Implement cart page request
- userPath = "/cart";
- // if checkout page is requested
- } else if (userPath.equals("/checkout")) {
- // TODO: Implement checkout page request
- // if user switches language
- } else if (userPath.equals("/chooseLanguage")) {
- // TODO: Implement language request
- }
- // use RequestDispatcher to forward request internally
- String url = "/WEB-INF/view" + userPath + ".jsp";
- try {
- System.out.println("Redirecting to : "+url);
- request.getRequestDispatcher(url).forward(request, response);
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement