【web.xml】

<session-config>
<session-timeout>30</session-timeout>
<cookie-config>
<!--<name>JSESSIONID</name>-->
<!--<domain>net.mypla</domain>-->
<!--<path>/shop</path>-->
<!--<comment><![CDATA[Keeps you logged in.See our privacy policy for more information]]></comment>-->
<http-only>true</http-only><!--Flash RIA不让访问-->
<!--<secure>false</secure>&lt;!&ndash;如果使用了HTTPS设为true&ndash;&gt;-->
<!--<max-age>1800</max-age>-->
</cookie-config>
<tracking-mode>COOKIE</tracking-mode><!--顺序很重要 首选cookie-->
<!--<tracking-mode>URL</tracking-mode>-->
<!--<tracking-mode>SSL</tracking-mode>-->
</session-config> 【StoreServlet】
@WebServlet(name="storeServlet",urlPatterns = {"/shop"})
public class StoreServlet extends HttpServlet{
private final Map<Integer,String> products = new Hashtable<>(); public StoreServlet(){
this.products.put(1,"Sandpaper");
this.products.put(2,"Nails");
this.products.put(3,"Glue");
this.products.put(4,"Paint");
this.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 "browse":
default:
this.browse(req,resp);
break;
}
} private void browse(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setAttribute("products",this.products);
req.getRequestDispatcher("/WEB-INF/jsp/view/browse.jsp").forward(req,resp);
} private void viewCart(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setAttribute("products",this.products);
req.getRequestDispatcher("/WEB-INF/jsp/view/viewCart.jsp").forward(req,resp); } private void addToCart(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
int productId;
try{
productId = Integer.parseInt(req.getParameter("productId"));
}catch (Exception e){
resp.sendRedirect("shop");
return;
} //cart的结构 <productId,qty>
//准备把购物车商品保存在session当中
HttpSession session = req.getSession();
//如果先前已经有
if(session.getAttribute("cart") == null) {
session.setAttribute("cart", new Hashtable<Integer, Integer>());
} @SuppressWarnings("unchecked")
Map<Integer,Integer> cart = (Map<Integer, Integer>) session.getAttribute("cart");
if (!cart.containsKey(productId)){
cart.put(productId,0);
}
cart.put(productId,cart.get(productId)+1); resp.sendRedirect("shop?action=viewCart");
}
} 【browse.jsp】
<h2>商品列表</h2>
<a href="<c:url value="/shop?action=viewCart"/>">View Cart</a><br /><br />
<%
@SuppressWarnings("unchecked")
Map<Integer,String> products = (Map<Integer, String>) request.getAttribute("products");
for (int id:products.keySet()){
%><a href="<c:url value="/shop"><c:param name="action" value="addToCart"/>
<c:param name="productId" value="<%=Integer.toString(id)%>"/>
</c:url>"><%=products.get(id)%><br /></a><%
}
%>
【viewCart.jsp】
<h2>查看购物车</h2>
<a href="<c:url value="/shop"/>">商品列表</a><br/><br/>
<%
@SuppressWarnings("unchecked")
Map<Integer,String> products = (Map<Integer, String>) request.getAttribute("products");
@SuppressWarnings("unchecked")
Map<Integer,Integer> cart = (Map<Integer, Integer>) session.getAttribute("cart"); if (cart == null || cart.size()==0){
out.println("购物车是空空的");
}else{
for (int id:cart.keySet()){
out.println(products.get(id)+"(数量: "+cart.get(id)+")<br/>");
}
}
%>
04-23 02:46