利用session实现简单购物车功能
本文实例为大家分享了利用session实现简单购物车功能的具体代码,供大家参考,具体内容如下
一、实现的功能
(1) 利用session实现购物车中的物品添加。
(2)使用servlet实现添加物品的功能(交互层)。
(3)一共有三个界面。第一个用来显示物品的列表的页面,第二个用来显示添物品的界面,第三个用来显示添加到购物车的信息页面。
二、代码实现
(1)物品列表页面:productlist.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Insert title here</title> </head> <body> <a href="<%=request.getContextPath()%>/shopping.pdo?pname='LENOVO R7'">联想拯救者R7</a> <br><br> <a href="<%=request.getContextPath()%>/shopping.pdo?pname='LENOVO R8'">联想拯救者R8</a> <br><br> <a href="<%=request.getContextPath()%>/shopping.pdo?pname='LENOVO R9'">联想拯救者R9</a> <br><br> <a href="<%=request.getContextPath()%>/shopping.pdo?pname='LENOVO R10'">联想拯救者R10</a> <br><br> </body> </html>
(2)添加购物车页面:producttails.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Insert title here</title> </head> <body> <% String pname = (String)request.getAttribute("p"); out.println(pname); %> <br><br> 拿到其他的......该产品的详细参数 <br><br> <a style="display: block;width: 100px ; height: 35px ;line-height :35px; text-decoration : none;background: red; color:#fff ;text-align: center;" href="<%=request.getContextPath() %>/addcart.pdo?pname=<%=pname %>" >加入购物车</a> </body> </html>
(3)显示添加成功后的信息页面:shoppingcart.jsp
<%@page import="java.util.List"%> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Insert title here</title> </head> <body> <% List<String> products = (List<String>)session.getAttribute("car"); for(String s: products){ out.print(s+"<br><br>"); } %> </body> </html>
(4)交互层:ShopController.java
package com.controller; import java.io.IOException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * Servlet implementation class ShopController */ @WebServlet(urlPatterns = {"*.pdo"}) public class ShopController extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ShopController() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //设置字符集 request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); response.setCharacterEncoding("text/html; charset=utf-8"); //在这个方法里边处理所有的增删改查的功能 String mn = request.getServletPath(); mn = mn.substring(1); mn = mn.substring(0 , mn.length()-4); //利用反射 try { //获取方法名,并且根据具体的去调用下边的方法 Method method = this.getClass().getDeclaredMethod(mn,HttpServletRequest.class , HttpServletResponse.class); method.invoke(this,request,response); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } private void shopping (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //设置字符集 request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); response.setCharacterEncoding("text/html; charset=utf-8"); String pname = request.getParameter("pname"); request.setAttribute("p", pname); request.getRequestDispatcher("/producttails.jsp").forward(request, response);; } private void addcart (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //设置字符集 request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); response.setCharacterEncoding("text/html; charset=utf-8"); String pname = request.getParameter("pname"); //添加购物车 HttpSession session = request.getSession(true); List<String> products = (List<String>)session.getAttribute("car"); if(products == null) { products = new ArrayList<>(); } //把添加的物品放入List集合 products.add(pname); //放入session中表示添加成功 session.setAttribute("car", products); //response.getWriter().print("添加成功!"); response.sendRedirect(request.getContextPath() + "/shoppingcart.jsp"); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)