大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
Session简单实现购物车功能
目前创新互联已为近1000家的企业提供了网站建设、域名、虚拟主机、网站托管运营、企业网站设计、梨树网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
这个小程序主要就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("书单
"); out.print("人类简史加入购物车
"); out.print("未来简史加入购物车
"); out.print("世界简史加入购物车
"); out.print("时间简史加入购物车
"); out.print("查看购物车
"); } 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(); Listlist = (List ) 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("已加入购物车
" + "继续购物
" + "查看购物车
"); } 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(); Listlist = (List )request.getSession().getAttribute("cart"); if(list!=null){ out.print("你的购物清单:
"); for (String string : list) { out.println(DBUtils.findById(string)+"
"); } out.println("
继续购物
"); }else{ out.println("你还没有将商品添加到购物车
" + "返回商品列表
"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
DBUtils:存储着商品信息
public class DBUtils { private static Mapmap = 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); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。