This is the only Servlet I have:
package com.tugay.shopping;
import javax.servlet.ServletException;
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 java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* User: koraytugay
* Date: 28/09/14
* Time: 14:29
*/
@WebServlet(name = "shoppingServlet", urlPatterns = "/shop")
public class ShoppingServlet extends HttpServlet {
final Map<Integer, String> products = new HashMap<>();
@Override
public void init() throws ServletException {
products.put(1, "Sandpaper");
products.put(2, "Nails");
products.put(3, "Glue");
products.put(4, "Paint");
products.put(5, "Tape");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String action = req.getParameter("action");
if (action == null)
action = "browse";
switch (action) {
case "addToCart":
this.addToCart(req, resp);
break;
case "viewCart":
this.viewCart(req, resp);
break;
case "empty":
this.empty(req, resp);
break;
case "browse":
this.browse(req, resp);
break;
}
}
private void empty(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getSession().removeAttribute("cart");
browse(req, resp);
}
@SuppressWarnings("unchecked")
private void viewCart(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Map<String, Integer> cart = (Map<String, Integer>) req.getSession().getAttribute("cart");
req.setAttribute("cart", cart);
req.getRequestDispatcher("/WEB-INF/jsp/view/viewCart.jsp")
.forward(req, resp);
}
@SuppressWarnings("unchecked")
private void addToCart(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Integer productId = Integer.parseInt(req.getParameter("productId"));
HttpSession session = req.getSession();
if (session.getAttribute("cart") == null) {
session.setAttribute("cart", new HashMap<String, Integer>());
}
Map<String, Integer> cart = (Map<String, Integer>) session.getAttribute("cart");
if (cart.containsKey(products.get(productId))) {
cart.put(products.get(productId), cart.get(products.get(productId)) + 1);
} else
cart.put(products.get(productId), 1);
resp.sendRedirect("shop?action=viewCart");
}
private void browse(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setAttribute("products", products);
req.getRequestDispatcher("/WEB-INF/jsp/view/browse.jsp").forward(req, resp);
}
}
viewCart.jsp:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<h1>You have these items in your cart:</h1>
<c:forEach items="${requestScope.cart}" var="cartItem">
${cartItem.key} - ${cartItem.value}
<br />
</c:forEach>
<a href="<c:url value="/shop"><c:param name="action" value="browse" /></c:url>">Back to Browse</a>
<a href="<c:url value="/shop"><c:param name="action" value="empty" /></c:url>">Empty Cart</a>
</body>
</html>
browse.jsp:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<h1>Click on the product to add product to your cart:</h1>
<c:forEach items="${requestScope.products}" var="product">
<a href="<c:url value="/shop">
<c:param name="action" value="addToCart"/>
<c:param name="productId" value="${product.key}"/>
</c:url>">${product.value}</a>
<br/>
</c:forEach>
</body>
</html>
web.xml:
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<el-ignored>false</el-ignored>
<page-encoding>UTF-8</page-encoding>
<scripting-invalid>false</scripting-invalid>
<include-prelude>/WEB-INF/jsp/base.jsf</include-prelude>
<trim-directive-whitespaces>true</trim-directive-whitespaces>
<default-content-type>text/html</default-content-type>
</jsp-property-group>
</jsp-config>
<session-config>
<session-timeout>30</session-timeout>
<cookie-config>
<http-only>true</http-only>
</cookie-config>
<tracking-mode>COOKIE</tracking-mode>
</session-config>
</web-app>