文件说明:
products.jsp ------ 购物车列表
buyServlet.java ------- 购物servlet
cart.jsp ----- 查看购物车
ClearServlet ---- 清空购物车
products.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%> <% 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>购物车</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"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <h3>商品出售列表</h3> 画家<a href="/Day0707/buy?id=1">购买</a><br/> 基地<a href="/Day0707/buy?id=2">购买</a><br/> 77169<a href="/Day0707/buy?id=3">购买</a><br/> 百度<a href="/Day0707/buy?id=4">购买</a><br/> 优酷<a href="/Day0707/buy?id=5">购买</a><br/> <hr/> <br/><a href='/Day0707/cart.jsp'>查看购物车</a> <br/><a href='/Day0707/clear'>清空购物车</a> </body> </html>
buyServlet.java
package com.gyarmy.session; import java.io.IOException; import java.io.PrintWriter; import java.util.HashMap; 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 BuyServlet extends HttpServlet { // 获取购买的商品信息 /* * * */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String id = request.getParameter("id"); String[] bookNames = { "画家", "基地", "77169", "百度", "优酷" }; String bookName = bookNames[Integer.parseInt(id) - 1]; Map<String, Integer> cart = (Map<String, Integer>) request.getSession() .getAttribute("cart"); if(cart == null){ cart = new HashMap<String, Integer>(); } if(cart.containsKey(bookName)){ //购买过 Integer count = cart.get(bookName); cart.put(bookName, count+1); }else{ //新购买 cart.put(bookName, 1); } //放入session中 request.getSession().setAttribute("cart", cart); response.setContentType("text/html;charset=utf-8"); response.getWriter().print("添加购物车成功!...<a href='/Day0707/products.jsp'>继续购买</a>"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
3 cart.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%> <% 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>购物车展示</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"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% //拿到购物车 Map<String,Integer> cart =(Map<String,Integer>) request.getSession().getAttribute("cart"); if(cart == null) { out.print("您还没有购买任何商品!..<a href='/Day0707/products.jsp'>继续购买</a>"); }else{ //遍历购物车,取出商品 out.print("购物商品如下:<br/>"); for (String key : cart.keySet()) { out.print("书本:"+ key + " 数量: " + cart.get(key)+"<br/>"); } out.print("<br/><a href='/Day0707/products.jsp'>继续购买</a>"); } %> </body> </html>
4 \clearServlet
package com.gyarmy.session; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ClearCartServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //清空session //request.getSession().invalidate(); request.getSession().removeAttribute("cart"); response.setContentType("text/html;charset=utf-8"); response.getWriter().print("清空购物车成功!<br/>"); response.getWriter().print("<a href='/Day0707/products.jsp'>继续购买</a>"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }
0则评论给“session的购物车实例”