CartServlet:
package bull03.Cart;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CartServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");
System.out.println(name);
Map<String,Integer> map = (Map<String, Integer>) request.getSession().getAttribute("cart");
if(map == null) {
map = new LinkedHashMap<String, Integer>();
}
if(map.containsKey(name)) {
Integer count = map.get(name);
count++;
map.put(name, count);
} else {
map.put(name, 1);
}
request.getSession().setAttribute("cart", map);
response.setContentType("text/html;charset=UTF-8");
response.getWriter().print("<h3><a href='/Web11/cart/product_list.jsp'>继续购物</a>|<a href='/Web11/cart/cart.jsp'>进行结算</a></h3>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
product_list.jsp:
<div class="col-md-2">
<a href="${pageContext.request.contextPath }/ProductServlet?id=1">
<img src="/Web11/history/products/1/cs10001.jpg" width="170" height="170" style="display: inline-block;">
</a>
<p><a href="/Web11/cart/product_info.htm" style='color:green'>冬瓜</a></p>
<p><font color="#FF0000">商城价:¥299.00</font></p>
</div>
(判断是否登录)
<%
User existUser = (User)request.getSession().getAttribute("existUser");
if(existUser == null) {
%>
<h2>您还没登录!请<a href="/Web11/cart/login.htm">登录</a></h2>
<%
} else {
%>
...
...
...
<%
}
%>
product_info.htm:
<div style="margin:20px 0 10px 0;;text-align: center;">
<a href="/Web11/CartServlet?name=111">
<input style="background: url('./images/product.gif') no-repeat scroll 0 -600px rgba(0, 0, 0, 0);height:36px;width:127px;" value="加入购物车" type="button">
</a> 收藏商品</div>
</div>
cart.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'cart.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<h1>购物车列表</h1>
<%
Map<String,Integer> map = (Map<String,Integer>)request.getSession().getAttribute("cart");
for(String product:map.keySet()) {
//获取购物车商品数量,一个product输出一次
Integer count = map.get(product);
%>
<h3>您购买的商品是:<%=product %> 商品数量为:<%=count %></h3>
<%
}
%>
</body>
</html>