基于Cookie的Session管理购物车

package 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;
import javax.servlet.http.HttpSession;


public class CartServlet extends HttpServlet {


/**

*/
private static final long serialVersionUID = 1L;


/**
* Constructor of the object.
*/
public CartServlet() {
super();
}


/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}


/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.

* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


response.setContentType("text/html;UTF-8");

/**
* 1.购物车Map<String,Integer> cart 
* 2.获取购物车,判断是否第一次访问
* 3.继续购物或结算
*/

//获取参数
String id = request.getParameter("id");

//购物车存入商品的名称和质量
String[] names = {"手电筒","冰箱","电视","洗衣机","电脑"};

//把id翻译名称
int idx = Integer.parseInt(id);
//商品名称
String name = names[idx-1];

//从session中获取购物车,先获取session
HttpSession session = request.getSession();

//从session中获取购物车
Map<String, Integer> cart = (Map<String, Integer>) session.getAttribute("cart");
//通过cart进行判断,是否是第一次访问
if(cart == null){
//创建购物车
cart = new HashMap<String, Integer>();
//第一次访问,放入名称和数量
cart.put(name, 1);
//存入到session中
session.setAttribute("cart", cart);
}else{
//是否包含改商品
if(cart.containsKey(name)){
//包含,取出数量加1加入购物车,存入session中
Integer count = cart.get(name);
count++;
//购物车存入商品
cart.put(name, count);
//存入的是购物车
session.setAttribute("cart", cart);
}else{
cart.put(name, 1);
}
}
//继续购物或结算
response.setContentType("text/html;UTF-8");
response.getWriter().write("<h3><a href='/day11/session/cartList.jsp'>继续购物</a><a href='/day11/session/pay.jsp'>去结算</a></h3>");
}


public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}


/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}


}



<%@ 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 'cartList.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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->


  </head>
  
  <body>
    <h3>手电筒<a href="/day11/cart?id=1">加入购物车</a></h3>
    <h3>冰箱<a href="/day11/cart?id=2">加入购物车</a></h3>
    <h3>电视<a href="/day11/cart?id=3">加入购物车</a></h3>
    <h3>洗衣机<a href="/day11/cart?id=4">加入购物车</a></h3>
    <h3>电脑<a href="/day11/cart?id=5">加入购物车</a></h3>
  </body>
</html>



<%@ 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 'pay.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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->


  </head>
  
  <body>
    <h3>clear</h3>
    <%
    //获取购物车
    Map<String, Integer> cart = (Map<String, Integer>)request.getSession().getAttribute("cart");
     
      //购物车不为空
      if(cart != null){
      //
      Set<String> keys = cart.keySet();
      for(String key : keys){
   %>  
   
   <h3>您买的商品是<%= key %>,数量是<%= cart.get(key) %></h3>
    <%
      }
      }else{
     
     %>
     <h3>您还没购物<a href="/day11/session/cartList.jsp">shopping</a></h3>
     <%
      }
     %>
  </body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值