利用session实现简单购物车功能
更新时间:2022年02月09日 09:56:14 作者:一只小小的蚂蚁
这篇文章主要为大家详细介绍了利用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"); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
Spring Boot应用中如何动态指定数据库实现不同用户不同数据库的问题
让我们创建一个 Spring Boot 项目首先设置一个具有必要依赖项的新 Spring Boot项目,在项目配置中包括 Spring Web、Spring Data JPA 和关于数据库的依赖项,接下来介绍Spring Boot应用中如何动态指定数据库,实现不同用户不同数据库的场景 ,需要的朋友可以参考下2024-04-04
最新评论