欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JAVAEE中用Session简单实现购物车功能示例代码

程序员文章站 2024-03-05 10:09:24
session简单实现购物车功能 这个小程序主要就3个页面,一个商品列表页面(homeservlet),一个是提示加入购物车页面(addcarttipservlet),一...

session简单实现购物车功能

这个小程序主要就3个页面,一个商品列表页面(homeservlet),一个是提示加入购物车页面(addcarttipservlet),一个是显示购物车清单页面(showcartservlet)。

homeservlet页面:

@webservlet({ "/homeservlet", "/home" })
public class homeservlet extends httpservlet {
  private static final long serialversionuid = 1l;

  public homeservlet() {
    super();
  }

  protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {

    request.setcharacterencoding("utf-8");
    response.setcontenttype("text/html;charset=utf-8");
    printwriter out = response.getwriter();
    out.print("<h2>书单</h2><hr/><br/>");
    out.print("人类简史<a href='"+request.getcontextpath()+"/addcarttip?id=1'>加入购物车</a><br/>");
    out.print("未来简史<a href='"+request.getcontextpath()+"/addcarttip?id=2'>加入购物车</a><br/>");
    out.print("世界简史<a href='"+request.getcontextpath()+"/addcarttip?id=3'>加入购物车</a><br/>");
    out.print("时间简史<a href='"+request.getcontextpath()+"/addcarttip?id=4'>加入购物车</a><br/>");
    out.print("<a href='"+request.getcontextpath()+"/show/cart'>查看购物车</a><br/>");
  }

  protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
    doget(request, response);
  }

}

addcarttipservlet页面:

@webservlet({ "/addcarttipsservlet", "/addcarttip" })
public class addcarttipsservlet extends httpservlet {
  private static final long serialversionuid = 1l;


  public addcarttipsservlet() {
    super();
  }

  protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {

    request.setcharacterencoding("utf-8");
    response.setcontenttype("text/html;charset=utf-8");

    httpsession session = request.getsession();
    list<string> list = (list<string>) session.getattribute("cart");
    if(list==null){
      list=new arraylist<>();
    }

    string id = request.getparameter("id");
    list.add(id);
    session.setattribute("cart", list);

    system.out.println(list.tostring());
    response.getwriter().println("已加入购物车<br/>"
        + "<a href='"+request.getcontextpath()+"/home'>继续购物</a><br/>"
        + "<a href='"+request.getcontextpath()+"/show/cart'>查看购物车</a><br/>");
  }

  protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
    doget(request, response);
  }

}

showcartsevlet页面

@webservlet({ "/showcartservlet", "/show/cart" })
public class showcartservlet extends httpservlet {
  private static final long serialversionuid = 1l;

  public showcartservlet() {
    super();
  }


  protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
    request.setcharacterencoding("utf-8");
    response.setcontenttype("text/html;charset=utf-8");
    printwriter out = response.getwriter();
    list<string> list = (list<string>)request.getsession().getattribute("cart");
    if(list!=null){   
      out.print("你的购物清单:<br/>");
      for (string string : list) {
        out.println(dbutils.findbyid(string)+"<br/>");
      }
      out.println("<br/><a href='"+request.getcontextpath()+"/home'>继续购物</a><br/>");
    }else{
      out.println("你还没有将商品添加到购物车<br/>"
          + "<a href='"+request.getcontextpath()+"/home'>返回商品列表</a><br/>");
    }
  }

  protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
    doget(request, response);
  }

}

dbutils:存储着商品信息

public class dbutils {
  private static map<string,string> map = new hashmap<>();

  static{
    map.put("1", "人类简史");
    map.put("2", "未来简史");
    map.put("3", "世界简史");
    map.put("4", "时间简史");
  }

  public static string findbyid(string id){
    return map.get(id);
  }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。