Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package assignment;
- import java.io.IOException;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.List;
- import javax.servlet.ServletConfig;
- import javax.servlet.ServletException;
- import javax.servlet.ServletRequest;
- 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 javax.websocket.Session;
- import assignment.ItemModel;
- import cs320.model.GuestBookEntry;;
- @WebServlet("/ShoppingCart")
- public class ShoppingCart extends HttpServlet {
- private static final long serialVersionUID = 1L;
- int numberOfItems = 0;
- int totalPrice = 0;
- List<ItemModel> cartItems = new ArrayList<ItemModel>();
- public ShoppingCart() {
- super();
- }
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- //Object cartItems = request.getAttribute("cartItems");
- request.getSession().getAttribute("cartItems");
- //System.out.println(cartItems);
- request.getSession().getAttribute("numberOfItems");
- request.getRequestDispatcher("/WEB-INF/ShoppingCart.jsp").forward(request, response);
- }
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- //List<ItemModel> cartItems = new ArrayList<ItemModel>();
- //List<ItemModel> cartItems = (List<ItemModel>) getServletContext().getAttribute("arrayList");
- //int numberOfItems = 0;
- Double price = Double.parseDouble(request.getParameter("price"));
- int quantity = Integer.valueOf(request.getParameter("quantity"));
- System.out.println(quantity);
- System.out.println(price);
- Integer id = Integer.valueOf(request.getParameter("id"));
- String name = request.getParameter("name");
- String details="";
- //cartItems.get(id).add(id, name, price, quantity,details);
- //ItemModel cart = new ItemModel(id, name, price, quantity, details);
- ItemModel cart = new ItemModel();
- cart.setId(id);
- cart.setName(name);
- cart.setPrice(price);
- cart.setQuantity(quantity);
- cart.setDetails(details);
- cartItems.add(cart);
- numberOfItems++;
- //numberOfItems = cartItems.size();
- System.out.println("number of items in cart: "+numberOfItems);
- //System.out.println(cartItems.toString());
- List<ItemModel> items = new ArrayList<ItemModel>();
- Connection c = null;
- try
- {
- String url = "jdbc:mysql://localhost/roughdb";
- String username = "root";
- String password = "mypass";
- /* String url = "jdbc:mysql://cs3.calstatela.edu/cs3220stu38";
- String username = "cs3220stu38";
- String password = "w5woPb7Z"; */
- c = DriverManager.getConnection( url, username, password );
- //String sql = "insert into shopping_cart (name, quantity, price, each_total) values (?,?,?,?) where id = ?";
- String sql = "INSERT INTO shopping_cart(id, name, quantity, price, each_total) VALUES(?, ?, ?, ?, ?)";
- PreparedStatement pstmt = c.prepareStatement(sql);
- //int totalPrice = 0;
- totalPrice += quantity*price;
- System.out.println("total: "+totalPrice);
- pstmt.setInt(1, id);
- pstmt.setString(2, name);
- pstmt.setInt(3, quantity);
- pstmt.setDouble(4, price);
- pstmt.setDouble(5, quantity*price);
- pstmt.executeUpdate();
- }
- catch( SQLException e )
- {
- throw new ServletException( e );
- }
- finally
- {
- try
- {
- if( c != null ) c.close();
- }
- catch( SQLException e )
- {
- throw new ServletException( e );
- }
- }
- request.getSession().setAttribute("numberOfItems", numberOfItems);
- //request.getSession().setAttribute("total", totalPrice);
- request.getSession().setAttribute("cartItems", cartItems);
- System.out.println("carItems: "+cartItems);
- doGet(request,response);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement