Simple Java web app code I wanted to work on HttpSessions and JSP.

This is the view I have:



&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;meta charset="utf-8"&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div&gt;
    &lt;c:out value="All available products:"/&gt;
    &lt;br/&gt;
    &lt;br/&gt;
    &lt;c:forEach items="${applicationScope.allProducts}" var="product"&gt;
        &lt;c:out value="${product}"/&gt;
        &amp;nbsp;
        &lt;a href="${pageContext.servletContext.contextPath}/shop?addProductbyName=&lt;c:out  value="${product}"/&gt;"&gt;Add to basket&lt;/a&gt;
        &lt;br/&gt;
    &lt;/c:forEach&gt;
    &lt;br/&gt;
    Currently your cart contains these items dude:
    &lt;br/&gt;

    &lt;c:forEach items="${sessionScope.cart}" var="product"&gt;
        &lt;c:out value="${product.key}"/&gt;
        &amp;nbsp;
        &lt;c:out value="${product.value}"/&gt;
        &lt;br/&gt;
    &lt;/c:forEach&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;


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&lt;String, Integer&gt; 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&lt;String, Integer&gt; doSessionStuff(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        HttpSession session = httpServletRequest.getSession();
        Map&lt;String, Integer&gt; cartMap = null;
        if (session.getAttribute("cart") == null) {
            cartMap = new HashMap&lt;String, Integer&gt;();
            session.setAttribute("cart", cartMap);
        }
        cartMap = (Map&lt;String, Integer&gt;) 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&lt;Integer,String&gt; products = new HashMap&lt;Integer, String&gt;();

    static {

        products.put(1,"Table");
        products.put(2,"Sofa");
        products.put(3,"Chair");
        products.put(4,"Window");

    }

    public static List&lt;String&gt; getAllProducts() {
        return new ArrayList&lt;String&gt;(products.values());
    }

}


This is what my page looks like:



If anyone wants to review this, they are welcome.
Interfaces vs implementations

Always use interfaces in declarations, not implementations. Instead of:


 private static HashMap&lt;Integer,String&gt; products = new HashMap&lt;Integer, String&gt;();



Do like this:

private static Map&lt;Integer,String&gt; products = new HashMap&lt;Integer, String&gt;();




doSessionStuff

The httpServletResponse parameter is unused, so drop it.

No need to initialize cartMap to null, as you will assign to it anyway. Actually you don't need that local variable at all, you could simplify the method without it:

@SuppressWarnings("unchecked")
private Map&lt;String, Integer&gt; doSessionStuff(HttpServletRequest httpServletRequest) {
    HttpSession session = httpServletRequest.getSession();
    if (session.getAttribute("cart") == null) {
        session.setAttribute("cart", new HashMap&lt;String, Integer&gt;());
    }
    return (Map&lt;String, Integer&gt;) session.getAttribute("cart");
}


doGet

You only use the cart variable if addProductbyName != null, so it would be better to move it inside the if block.

It's a bit awkward to do cart.put(addProductbyName, 0); and then again cart.put(addProductbyName, cart.get(addProductbyName) + 1);. And you are doing cart.get(addProductbyName) multiple times.

The @SuppressWarnings("unchecked") is also unnecessary for this method, as there are no unchecked casts.

The method would be cleaner like this:

@Override
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
        throws ServletException, IOException {
    String addProductbyName = httpServletRequest.getParameter("addProductbyName");
    if (addProductbyName != null) {
        Map&lt;String, Integer&gt; cart = doSessionStuff(httpServletRequest);
        Integer count = cart.get(addProductbyName);
        if (count == null) {
            cart.put(addProductbyName, 1);
        } else {
            cart.put(addProductbyName, count + 1);
        }
    }

    httpServletResponse.sendRedirect(getServletContext().getContextPath()+"/shopping.jsp");
}