1. AJAVA(2160707) 140120107100
Page No: 1
Report on Open Ended Problem (OEP)
(Academic Year: 2016-2017)
Subject Code: 2160707
Subject Name: Advanced Java
Definition of OEP: Create a shopping application using Servlet technology to display all the
times added in the cart. Design the following pages:-
a. Home page containing the gallery of items in the form of thumbnails. Below each
thumbnail display the price and item code for that item. Also there must be checkbox
displaying the text to add to cart below the prices of each item. User can click on multiple
checkboxes to add the item in his cart. At the bottom of the page there must be a submit
button which when clicked, control must be redirected to another page.
b. Display page must add the selected item’s code and its price in cookie and read those
values from cookie to display those items that are selected from the previous page. Also
display the details of each item (i.e. item code and price) in a tabular form.
Submitted By
Enrollment Number: 140120107100
Name: Siddharth Panchal
Branch: CE
Division & Batch: B-B2
Guided By
Name of Faculty: Prof. Mukesh Parmar
Department: CE
2. AJAVA(2160707) 140120107100
Page No: 2
INDEX
1. Definition:
Create a shopping application using Servlet technology to display all the times added in the cart.
Design the following pages:-
a. Home page containing the gallery of items in the form of thumbnails. Below each
thumbnail display the price and item code for that item. Also there must be checkbox
displaying the text to add to cart below the prices of each item. User can click on multiple
checkboxes to add the item in his cart. At the bottom of the page there must be a submit
button which when clicked, control must be redirected to another page.
b. Display page must add the selected item’s code and its price in cookie and read those
values from cookie to display those items that are selected from the previous page. Also
display the details of each item (i.e. item code and price) in a tabular form.
2. Implementation Code:
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
</head>
<body>
<table border="1px">
<form action="addtocart">
<th>Pizza Name</th> <th>Price</th> <th>Add to Cart</th>
<tr><td>Muffuleta</td><td>$20</td><td><input type="hidden" name="name"
value="Muffuleta">
<input type="hidden" name="price" value="20"><input type="submit" value="Add to
cart"></td>
</tr>
</form>
<form action="addtocart">
<tr><td>Veggie Delight</td><td>$40</td><td>
<input type="hidden" name="name" value="Veggie Delight">
<input type="hidden" name="price" value="40"><input type="submit" value="Add to
cart"></td>
</tr>
</form>
3. AJAVA(2160707) 140120107100
Page No: 3
<form action="addtocart">
<tr><td>Margherita</td><td>$10</td><td>
<input type="hidden" name="name" value="margherita">
<input type="hidden" name="price" value="10"><input type="submit" value="Add to
cart"></td>
</tr>
</form>
</table>
</body>
</html>
import java.util.HashMap;
/**
*
* @author rajat
*/
public class Cart {
HashMap<String, Integer> cartItems;
public Cart(){
cartItems = new HashMap<>();
}
public HashMap getCartItems(){
return cartItems;
}
public void addToCart(String itemId, int price){
cartItems.put(itemId, price);
}
}
Shopping Cart Servlet
public class Addtocart extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
HttpSession session = request.getSession();
cart shoppingCart;
shoppingCart = (cart) session.getAttribute("cart");
if(shoppingCart == null){
shoppingCart = new cart();
session.setAttribute("cart", shoppingCart);
}
String name = request.getParameter("name");
Integer price = Integer.parseInt(request.getParameter("price"));
shoppingCart.addToCart(name, price);
session.setAttribute("cart", shoppingCart);
4. AJAVA(2160707) 140120107100
Page No: 4
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>result</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Pizza successfully added to cart </h1>");
out.println("<form action='index.html'>Add more pizza item<input type='submit'
value='go'></form>");
out.println("<hr>");
out.println("<h2>Cart</h2>");
HashMap<String, Integer> items = shoppingCart.getCartItems();
out.println("<table border='1px'>");
for(String key: items.keySet()){
out.println("<tr><td>"+key+" - </td><td>"+"$"+items.get(key)+"</td></tr>");
}
out.println("<table>");
out.println("</body>");
out.println("</html>");
}
}
To delete items from the shopping cart
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String name = request.getParameter("name");
HttpSession session = request.getSession();
cart shoppingCart;
shoppingCart = (cart) session.getAttribute("cart");
shoppingCart.deleteFromCart(name);
session.setAttribute("cart", shoppingCart);
shoppingCart = (cart) session.getAttribute("cart");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet deleteItem</title>");
out.println("</head>");
out.println("<body>");
HashMap<String, Integer> items = shoppingCart.getCartItems();
out.println("<table border='1px'>");
for(String key: items.keySet()){