Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Caerservlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@


import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/cart")
public class Caerservlet extends HttpServlet {

/**
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
Cart cart = (Cart) request.getSession().getAttribute("cart");
if (cart == null)
{
cart = new Cart();
request.getSession().setAttribute("cart", cart);
}
request.getRequestDispatcher("cart.jsp").forward(request, response);
}
}
41 changes: 41 additions & 0 deletions Cart.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

import java.util.*;


public class Cart
{
private List<Product> items;

public Cart()
{
items = new ArrayList<>();
}

public void addItem(Product product)
{
items.add(product);
}
public void removeItem(Product product)
{
items.remove(product);
}



public List<Product> getItems() {
return items;
}

public double getTotalPrice()
{
double total = 0.0;
for (Product product : items)
{
total += product.getPrice();
}
return total;
}
}



13 changes: 13 additions & 0 deletions Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author LENOVO
*/
class Product {

}
22 changes: 22 additions & 0 deletions Productservlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import java.util.*;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/products")
public class Productservlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Product> products = getProductListFromDatabase(); // Retrieve product list from database
request.setAttribute("products", products);
request.getRequestDispatcher("product-list.jsp").forward(request, response);
}

private List<Product> getProductListFromDatabase() {
return null;
// Retrieve product list from the database and return it
}
}
7 changes: 7 additions & 0 deletions Shop.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

public class Shop
{
private String title;
private double price;

}
27 changes: 27 additions & 0 deletions cart.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<title>Shopping Cart</title>
</head>
<body>
<h1>Shopping Cart</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<% for (Product product : ((Cart) request.getSession().getAttribute("cart")).getItems()) { %>

<td><%= product.getName() %></td>
<td><%= product.getPrice() %></td>

<% } %>
</tbody>
</table>
<p>Total Price: <%= ((Cart) request.getSession().getAttribute("cart")).getTotalPrice() %></p>
</body>
</html>
16 changes: 16 additions & 0 deletions productlist.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<title>Product List</title>
</head>
<body>
<h1>Product List</h1>
<ul>
<% for (Product product : (List<Product>) request.getAttribute("products")) { %>
<li><%= product.getName() %> - <%= product.getPrice() %></li>
<% } %>
</ul>
<a href="cart">View Cart</a>
</body>
</html>