欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

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

时间:2020-10-04 14:43:56|栏目:JAVA代码|点击:

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);
  }

}

上一篇:详解Spring Cloud Zuul重试机制探秘

栏    目:JAVA代码

下一篇:Java实现的可选择及拖拽图片的面板功能【基于swing组件】

本文标题:JAVAEE中用Session简单实现购物车功能示例代码

本文地址:http://www.codeinn.net/misctech/6830.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有