Advertisement
Guest User

Untitled

a guest
Dec 1st, 2016
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. package assignment;
  2.  
  3. import java.io.IOException;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12.  
  13. import javax.servlet.ServletConfig;
  14. import javax.servlet.ServletException;
  15. import javax.servlet.ServletRequest;
  16. import javax.servlet.annotation.WebServlet;
  17. import javax.servlet.http.HttpServlet;
  18. import javax.servlet.http.HttpServletRequest;
  19. import javax.servlet.http.HttpServletResponse;
  20. import javax.servlet.http.HttpSession;
  21. import javax.websocket.Session;
  22.  
  23. import assignment.ItemModel;;
  24.  
  25. @WebServlet("/ShoppingCart")
  26. public class ShoppingCart extends HttpServlet {
  27. private static final long serialVersionUID = 1L;
  28. List<ItemModel> cartItems = new ArrayList<ItemModel>();
  29.  
  30. public ShoppingCart() {
  31. super();
  32. }
  33.  
  34. public void init(ServletConfig config) throws ServletException {
  35. super.init(config);
  36. try {
  37. Class.forName("com.mysql.jdbc.Driver");
  38. } catch (ClassNotFoundException e) {
  39. throw new ServletException(e);
  40. }
  41. }
  42. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  43.  
  44. //String cartItems = String(request.getAttribute("cartItems"));
  45. // Object cartItems = request.getServletContext().getAttribute("cartItems");
  46. // System.out.println(cartItems);
  47. // request.getRequestDispatcher("/WEB-INF/ShoppingCart.jsp").forward(request, response);
  48. //HttpSession session = request.getSession();
  49. //Object cartItems = session.getAttribute("cartItems");
  50. request.getSession().getAttribute("cartItems");
  51. System.out.println(cartItems);
  52. request.getRequestDispatcher("/WEB-INF/ShoppingCart.jsp").forward(request, response);
  53. }
  54.  
  55. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  56. //List<ItemModel> cartItems = new ArrayList<ItemModel>();
  57.  
  58. Double price = Double.parseDouble(request.getParameter("price"));
  59. int quantity = Integer.valueOf(request.getParameter("quantity"));
  60. System.out.println(quantity);
  61. System.out.println(price);
  62. Integer id = Integer.valueOf(request.getParameter("id"));
  63. String name = request.getParameter("name");
  64. String details="";
  65. //cartItems.get(id).add(id, name, price, quantity,details);
  66. ItemModel cart = new ItemModel(id, name, price, quantity, details);
  67. cart.setId(id);
  68. cart.setName(name);
  69. cart.setPrice(price);
  70. cart.setQuantity(quantity);
  71. cart.setDetails(details);
  72. //System.out.println(cartItems.toString());
  73.  
  74. Connection c = null;
  75. try
  76. {
  77. String url = "jdbc:mysql://cs3.calstatela.edu/cs3220stu49";
  78. String username = "cs3220stu49";
  79. String password = "#CKr.Npn";
  80.  
  81. //List<ItemModel> items = new ArrayList<ItemModel>();
  82. c = DriverManager.getConnection( url, username, password );
  83.  
  84. //String sql = "insert into shopping_cart (name, quantity, price, each_total) values (?,?,?,?) where id = ?";
  85. String sql = "INSERT INTO shopping_cart(id, name, quantity, price, each_total) VALUES(?, ?, ?, ?, ?)";
  86.  
  87. PreparedStatement pstmt = c.prepareStatement(sql);
  88. pstmt.setInt(1, id);
  89. pstmt.setString(2, name);
  90. pstmt.setInt(3, quantity);
  91. pstmt.setDouble(4, price);
  92. pstmt.setDouble(5, quantity*price);
  93. pstmt.executeUpdate();
  94. }
  95. catch( SQLException e )
  96. {
  97. throw new ServletException( e );
  98. }
  99. finally
  100. {
  101. try
  102. {
  103. if( c != null ) c.close();
  104. }
  105. catch( SQLException e )
  106. {
  107. throw new ServletException( e );
  108. }
  109. }
  110. //response.sendRedirect("Store");
  111. //session.setAttribute("cartItems", cartItems);
  112. //request.getRequestDispatcher("/WEB-INF/ShoppingCart.jsp").forward(request, response);
  113. cartItems.add(cart);
  114. request.getSession().setAttribute("cartItems", cartItems);
  115. //doGet(request,response);
  116. response.sendRedirect("ShoppingCart");
  117. }
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement