Advertisement
Guest User

Untitled

a guest
Jun 28th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. @WebServlet(name = "ControllerServlet",
  2. loadOnStartup = 1,
  3. urlPatterns = {
  4. "/category",
  5. "/addToCart",
  6. "/viewCart",
  7. "/updateCart",
  8. "/checkout",
  9. "/purchase",
  10. "/chooseLanguage"})
  11.  
  12. @EJB
  13. private CategoryFacade categoryFacade;
  14. @EJB
  15. private OrderManager orderManager;
  16. @Override
  17. public void init() throws ServletException {
  18.  
  19. // store category list in servlet context
  20. getServletContext().setAttribute("categories", categoryFacade.findAll());
  21. }
  22.  
  23. /**
  24. * Handles the HTTP <code>GET</code> method.
  25. * @param request servlet request
  26. * @param response servlet response
  27. * @throws ServletException if a servlet-specific error occurs
  28. * @throws IOException if an I/O error occurs
  29. */
  30. @Override
  31. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  32. throws ServletException, IOException {
  33.  
  34. String userPath = request.getServletPath();
  35.  
  36. // if category page is requested
  37. if (userPath.equals("/category")) {
  38. // get categoryId from request
  39. String categoryId = request.getQueryString();
  40.  
  41. if (categoryId != null) {
  42. // get selected category
  43. Category selectedCategory = categoryFacade.find(Short.parseShort(categoryId));
  44.  
  45. // place selected category in request scope
  46. request.setAttribute("selectedCategory", selectedCategory);
  47. // get all products for selected category
  48. Collection<Product> categoryProducts = selectedCategory.getProductCollection();
  49.  
  50. // place category products in request scope
  51. request.setAttribute("categoryProducts", categoryProducts);
  52. }
  53.  
  54. // if cart page is requested
  55. } else if (userPath.equals("/viewCart")) {
  56. // TODO: Implement cart page request
  57.  
  58. userPath = "/cart";
  59.  
  60. // if checkout page is requested
  61. } else if (userPath.equals("/checkout")) {
  62. // TODO: Implement checkout page request
  63.  
  64. // if user switches language
  65. } else if (userPath.equals("/chooseLanguage")) {
  66. // TODO: Implement language request
  67.  
  68. }
  69.  
  70. // use RequestDispatcher to forward request internally
  71. String url = "/WEB-INF/view" + userPath + ".jsp";
  72.  
  73. try {
  74. System.out.println("Redirecting to : "+url);
  75. request.getRequestDispatcher(url).forward(request, response);
  76. } catch (Exception ex) {
  77. ex.printStackTrace();
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement