I wanted to work on HttpSessions
and JSP.
This is the view I have:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<div>
<c:out value="All available products:"/>
<br/>
<br/>
<c:forEach items="${applicationScope.allProducts}" var="product">
<c:out value="${product}"/>
<a href="${pageContext.servletContext.contextPath}/shop?addProductbyName=<c:out value="${product}"/>">Add to basket</a>
<br/>
</c:forEach>
<br/>
Currently your cart contains these items dude:
<br/>
<c:forEach items="${sessionScope.cart}" var="product">
<c:out value="${product.key}"/>
<c:out value="${product.value}"/>
<br/>
</c:forEach>
</div>
</body>
</html>
And the Servlet:
package com.tugay;
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: 20/07/14
* Time: 18:07
*/
@WebServlet(urlPatterns = "/shop", name = "shoppingServlet")
public class ShopServlet extends HttpServlet {
@Override
public void init() throws ServletException {
if (getServletContext().getAttribute("allProducts") == null) {
getServletContext().setAttribute("allProducts", Database.getAllProducts());
}
}
@SuppressWarnings("unchecked")
@Override
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
throws ServletException, IOException {
Map<String, Integer> cart = doSessionStuff(httpServletRequest, httpServletResponse);
String addProductbyName = httpServletRequest.getParameter("addProductbyName");
if (addProductbyName != null) {
if (cart.get(addProductbyName) == null) {
cart.put(addProductbyName, 0);
}
cart.put(addProductbyName, cart.get(addProductbyName) + 1);
}
httpServletResponse.sendRedirect(getServletContext().getContextPath()+"/shopping.jsp");
}
@SuppressWarnings("unchecked")
private Map<String, Integer> doSessionStuff(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
HttpSession session = httpServletRequest.getSession();
Map<String, Integer> cartMap = null;
if (session.getAttribute("cart") == null) {
cartMap = new HashMap<String, Integer>();
session.setAttribute("cart", cartMap);
}
cartMap = (Map<String, Integer>) session.getAttribute("cart");
return cartMap;
}
}
And the "Mock" Db:
package com.tugay;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* User: koraytugay
* Date: 20/07/14
* Time: 17:58
*/
public class Database {
private static HashMap<Integer,String> products = new HashMap<Integer, String>();
static {
products.put(1,"Table");
products.put(2,"Sofa");
products.put(3,"Chair");
products.put(4,"Window");
}
public static List<String> getAllProducts() {
return new ArrayList<String>(products.values());
}
}
This is what my page looks like:
If anyone wants to review this, they are welcome.