import java.io.IOException; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ShoppingCartViewerRewrite extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.println("Current Shopping Cart Items"); out.println(""); // Get the current session ID, or generate one if necessary String sessionid = req.getPathInfo(); if (sessionid == null) { sessionid = generateSessionId(); } // Cart items are associated with the session ID String[] items = getItemsFromCart(sessionid); // Print the current cart items. out.println("You currently have the following items in your cart: "); if (items == null) { out.println("None"); } else { out.println("
"); for (int i = 0; i < items.length; i++) { out.println("
" + items[i]); } out.println("
"); } // Ask if the user wants to add more items or check out. // Include the session ID in the action URL. out.println(""); // Offer a help page. Include the session ID in the URL. out.println("For help, click + "?topic=ShoppingCartViewerRewrite\">here"); out.println("